Search code examples
batch-filecompression7zip

How to add Multiple Files & Folders to a single 7zip archive using batch


Here's what I am trying to do,

I would like to create a batch file to compress multiple folders and files into one 7zip file for archival purposes.

I have external drives we can call [SOURCE DISK] and [DESTINATION DISK].

The File & Folders I want to compress are in the root of the [SOURCE DISK]. I would like to add all these folders and the file to a single 7zip archive with a designated name [ARCHIVE NAME].

I have 7zip 64bit installed.

Here's what I have tried;

C:\Program Files\7-Zip\7za a -tzip "[DESTINATION DISK]\[ARCHIVE NAME] %DATE:~12,2%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%.zip" "[SOURCE DISK]\[FIRST FOLDER], [SOURCE DISK]\[SECOND FOLDER], [SOURCE DISK]\[THIRD FOLDER], [SOURCE DISK]\[AN EXCEL DOCUMENT]" -mx5

C:\Program Files\7-Zip\7za a -tzip "[DESTINATION DISK]\[ARCHIVE NAME].zip" "[SOURCE DISK]\[FIRST FOLDER], [SOURCE DISK]\[SECOND FOLDER], [SOURCE DISK]\[THIRD FOLDER], [SOURCE DISK]\[AN EXCEL DOCUMENT]" -mx5

C:\Program Files\7-Zip\7za a -tzip "[DESTINATION DISK]\[ARCHIVE NAME].zip" "[SOURCE DISK]\[FIRST FOLDER]" -mx5

c:\Program Files\7-Zip\7z.exe a -tzip "[DESTINATION DISK]\[ARCHIVE NAME].zip" "[SOURCE DISK]\[FIRST FOLDER]" -mx5

c:\Program Files\7-Zip\7z.exe a -tzip "[DESTINATION DISK]\[ARCHIVE NAME].7z" "[SOURCE DISK]\[FIRST FOLDER]" -mx5

I am new to 7zip command line. I'm sure I am missing something crucial. When I manually execute the batch file a cmd prompt flickers but it seams nothing happens as my CPU usage doesn't steadily change as it would if I were to use the 7zip GUI. I believe the cmd prompt is flickering because it's started but there was a syntax error.

In the past I've used

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

to compress every folder within a current directory and it seamed to work nicely.

How can I specify Folders and Files to include in one archive using a batch.

Any tips will be greatly appreciated.


Solution

  • "C:\Program Files\7-Zip\7z.exe" a -pSECRET "[DESTINATION DISK]\[ARCHIVE NAME] %DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%.7z" "[SOURCE DISK][FIRST FOLDER]" [SOURCE DISK][SECONDFOLDER] "[SOURCE DISK]\[EXCEL DOCUMENT]" -mhe=on -mx5
    

    I figured it out. Thanks for the support! After a bunch of reading and trial and error, I realized if the path of the source or destination includes a space: quotations are required. And if there is no space: don't include quotations. See the example above. Hope this helps others in the future.

    Not to confuse, but I've added a password as well as hiding the file names before the password is entered. I do realize the password will be in the .bat but these archives will be transferred to other machines and the .bat will not be available to those in the future.