I'd like to create a batch file to .7z the following below. So the batch.bat will be in the root folder where it will be executed and will zip the files inside folder1, folder2, folder3, etc., but the .7z won't create a folder. It just needs to zip the files in folders and create a separate .7z file with the title of that folder, but not create .7z with a folder.
root (batch.bat)
folder1
- file1.ext
folder2
- file1.ext
- file2.ext
folder3
- file1.ext
- file2.ext
- file3.ext
This is the code I'm using to create a .7z of single files.
PATH %%PATH%%;"C:\Program Files\7-Zip";
FOR %%I IN (*.*) DO 7z.exe a -t7z -m0=LZMA2 -mx=5 -mmt=ON "%%~nI.7z" "%%I"
You need a for /D
loop that iterates through the directories in \root
, which is wrapped around the 7z.exe
tool, like this:
for /D %%I in ("\root\*") do (
pushd "%%~fI"
"%ProgramFiles%\7-Zip\7z.exe" a -t7z -m0=LZMA2 -mx=5 -mmt=ON "%%~dpnxI.7z" ".\*"
popd
)
Since 7z.exe
seems to store paths relative to the current working directory, I temporarily switch to every iterated one using pushd
and popd
.
I recommend to specify the \root
path in an absolute manner (for example, D:\Data
), so the batch program works from everywhere. However, if you do not want that, replace the \root\*
part by *
.
By the way, the path
command is not needed when you specify the path directly when actually using 7z.exe
, like I did above (using the system variable %ProgramFiles%
rather than a dedicated directory to which it points anyway).
However, your syntax is wrong anyway: you are actually setting the PATH
variable to %PATH%;"C:\Program Files\7-Zip";
literally, but I suppose you simply want to append "C:\Program Files\7-Zip"
to it, for what you needed to write PATH %PATH%;"%ProgramFiles%\7-Zip"
.