Kenny
Kenny

Reputation: 353

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.

Upvotes: 0

Views: 12389

Answers (4)

o.v
o.v

Reputation: 885

You can use text file containing a list of source paths as described here.

Create list.txt with paths:

My programs\*.cpp
Src\*.cpp

Then use this file in the archive command:

7z a archive.7z @list.txt

Upvotes: 0

Kenny
Kenny

Reputation: 353

"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.

Upvotes: 1

Ben Personick
Ben Personick

Reputation: 3264

You just need to have the correct options specified and make sure all of your paths are quoted

@(
  SetLocal
  Echo Off
  SET "_Src="C:\Source\Path"
)

REM Get The Date Tokens and Time in ISO Format:

FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN (
  "%DATE% %TIME: =0%"
) DO (
  FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('
    ECHO.^| DATE
  ') DO (
    SET "%%~a=%%~A"
    SET "%%~b=%%~B"
    SET "%%~c=%%~C"
    SET "IsoTime=%%~D.%%~E.%%~F.%%~G"
  )
)

REM Set Archive Filename with Date:
SET "_Dst="D:\Destination\Path\ARCHIVE_NAME_%yy%-%mm%-%dd%_%IsoTime%.zip"

REM Run 7Zip Command:
"C:\Program Files\7-Zip\7z.exe" a -bd -tZip "%_Dst%" "%_Src%"

(
  EndLocal
  Exit /B
)

I out in a method to get the Date into ISO format that is region independent. as well since you have a different date/time format on your system then I do. You can change it around as you like.

Upvotes: 0

Magoo
Magoo

Reputation: 80023

C:\Program Files\7-Zip\7za attempts to execute C:\Program. You need to quote it "C:\Program Files\7-Zip\7za" as you did in your "I've used" example.

Possibly one should draw your attention to the -r switch to recurse the archiving - and I believe all of the switches (like -mx5) should appear before the archive filename, otherwise it will assume that -mx5 is a filename to be archived.

Upvotes: 0

Related Questions