# Batch archive multiple folders with 7-zip to other directory

In reference to this wonderful work:

@echo off
if [%1]==[] goto :eof
:loop
7z a -tzip "%~1.zip" "%~1"
shift
if not [%1]==[] goto loop


I can't for the heck of it figure out where to put the output directory. I want to batch archive from my external hard drives, most of which are completely filled up. So I need to change the output directory to another drive.

I managed to find a way to do it with the following command line:

for /d %X in (*) do "c:\Program Files\7-Zip\7z.exe" a -tzip "<destinationPath>\%X.zip" "%X\"


However the above batch file would really come in handy because I could simply drop as many folders onto it as I have spare storage space, then move the archives and proceed with the next batch.

On Windows here by the way so please bear with me.

The 7z command in the batch file you provided uses the full path of the input file to get the output file.

In order to change this, you can change "%~1.zip" to "<destinationPath>\%~nx1.zip" as shown below or to "<destinationPath>\%~n1.zip" if you don't need the extension of the original file to be in the new filename.

@echo off
if [%1]==[] goto :eof
:loop
7z a -tzip "<destinationPath>\%~nx1.zip" "%~1"
shift
if not [%1]==[] goto loop


For clarification:

• %~1 represents the fully qualified path name of the first input (ex: C:\my\path\text.txt)
• %~n1 represents the filename only of the first input (ex: text)
• %~x1 represents the extension only of the first input (ex: .txt)
• These two are combined into %~nx1, which represents the filename and extension of the first input (ex: test.txt)
• Sensational! Where do you learn this stuff? I searched all over the place. Is there a definitive batch/cmd guide or something? – FOSSFreak Aug 29 '18 at 17:54
• I've found Rob Van Der Woude and SS64 to be good references. Some of these commands are also in unintuitive places. The information in this answer is from the FOR /? command. – Worthwelle Aug 29 '18 at 18:01
• Thank you kindly, I have marked down these resources for future references. – FOSSFreak Aug 29 '18 at 18:07