How can I create a batch job (.bat) that zip a folder?

Posted on

Problem :

From 7zip I’ve downloaded their command line tool 7za.exe, and I have to following script:

echo off

cls

set zip ="C:7za.exe"

set filename="%date%.backup.zip"

set pathtobackup="C:MyDocs"

%zip% -t7z %filename% %pathtobackup%*.doc

My intention is to backup all the doc files in MyDocs folder (including subfolders) and put them in a dated zip file. Afterwards I will move this file with robocopy to my server, but at the moment Im having some problems getting it to zip the files.

Anyone have a clue? Anyway I can append a password for the file too?

Solution :

Use the syntax

7z a -tzip archive.zip -r src*.cpp src*.h

so in your case, it’d be

7z a -t7z %filename% -r %pathtobackup%*.doc

Source: http://sevenzip.sourceforge.jp/chm/cmdline/switches/recurse.htm

Install 7z and you can use this working example with delete script for files older than 7 days. What this script does, it zips the files from given ‘input’ folder to ‘output’ folder. In order to zip folders, you have to add /d or to zip all files within folder just modify the last “%%X”.

REM This is a batch script to zip files in folder or add /d to zip folders.
REM This script is using 7z to zip files, installation is required - "[http://www.7-zip.org/a/7z938-x64.msi][1]"
REM Remember to point 'do' to 7z installed path 7z.exe

SET input1=D:BackupSampleFolder
SET input2=D:BackupSampleFolder2

SET output1=C:ZipBackupsSampleFolder
SET output2=C:ZipBackupsSampleFolder2

CD /d %input1%
for %%X in (*) do "C:Program Files7-Zip7z.exe" a "%output1%%%X.zip" "%%X"
FORFILES /P "%input1%" /M *.* /D -8 /C "cmd /c del @file"

CD /d %input2%
for %%X in (*) do "C:Program Files7-Zip7z.exe" a "%output2%%%X.zip" "%%X"
FORFILES /P "%input2%" /M *.* /D -8 /C "cmd /c del @file"

I have found a simple solution for this. Suppose you have multiple sub folders to be zipped in a folder. In that case,
1. Download and lnstall 7zip software. Then copy 7z.exe to the particular folder where you want to zip your subfolders.

in a notepad, copy and paste the following and save as .bat file. :

cd "your_Folder_path" for /d %%f in (*) do (7z a -t7z %%f.7z "your_Folder_path%%f")

The double click to run the file.

In case you want to delete the original folders,
add this before the closing brace :

rd /s /q "your_Folder_path%%f"

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%

set year=%date:~-4%
echo year=%year%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%

set datetimef=%day%_%month%_%year%___%hour%_%min%_%secs%

echo datetimef=%datetimef%


path %path%;"C:Program Files7-Zip"

Leave a Reply

Your email address will not be published. Required fields are marked *