0

I'm trying to compress multiple separate folders to .zip (whether it be 7zip or PowerShell or tar) as well as being portable (as in the .bat can be anywhere) yes I looked at other posts but none of them is the answer to what I'm trying to do (if it's even possible)

mainfolder is where the .bat is

before compression

mainfolder | folder1 | folder .txt .json .png

this is what I want after compression

mainfolder | folder1.zip | folder .txt .json .png

but what I get is

mainfolder | folder1.zip | folder1 | folder .txt .json .png

I don't what the folder1 in the .zip

so is it possible to zip it without that other folder?

1

2 Answers 2

0

This is the same for any zip tool, but let's say you're using Powershell, and your folder structure looks like: C:\Folder1\Folder2\myFolders\myFile.txt:

# this zip will have a top-level folder (folder2) like you've seen
Compress-Archive -Path "C:\folder1\folder2" -Destination "C:\zip\file.zip"

# add an asterisk to take everything inside the target folder but leave the folder itself out:
Compress-Archive -Path "C:\folder1\folder2\*" -Destination "C:\zip\file.zip"
0

As an example using tar.exe in batch script:

@Echo Off
set "fol=C:\Temp\folder1"     & rem folder to zipped 
set "zip=C:\Temp\folder1.zip" & rem location of zip
pushd "%fol%" 2>Nul&& (tar.exe -cf "%zip%" "*" >Nul& popd)
  • The zip file will no additional root folder1.
  • Replace set "fol=... and set "zip=... as you wish.
  • Batch script can be placed anywhere.
  • To check whether tar.exe exists or not: where.exe tar.exe
  • more options tar.exe --help

Alternative using 7z:

@Echo Off
set "fol=C:\Temp\folder1"     & rem folder to zipped 
set "zip=C:\Temp\folder1.zip" & rem location of zip
set "sev=C:\Program Files\7-Zip\7z.exe"
"%sev%" a -tzip "%zip%" "%fol%\*"
  • a -tzip archive type (-t7z/-trar/...)
5
  • it works but I can't open it by clicking on it as it says invalid yet I can open it with 7zip
    – Jimmy King
    Commented Jun 6 at 16:35
  • If 7z is installed, it is better to use it which has more complete features. Or If PowerShell script, use Compress-Archive like @Cpt.Whale answer.
    – Mr.Key7
    Commented Jun 6 at 16:42
  • I might rather use 7z but how can I zip multiple files while also keeping the names it will also be portable as in it will be anywhere and still work
    – Jimmy King
    Commented Jun 6 at 18:27
  • What does as portable mean?
    – Mr.Key7
    Commented Jun 6 at 19:08
  • I mean like if someone else uses the .bat it can download files to that spot and not to a specific folder it just does everything right where it is like in download or the desktop
    – Jimmy King
    Commented Jun 6 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .