How to Automatically Zip Files with a Batch File
If you have to do some repetitive tasks on a daily basis, you probably know how tedious it can get, especially if you have to follow multiple steps over and over again. Zipping files is definitely one of those types of tasks, but the good news is that it doesn’t have to be. There are solutions out there that will automate compressing files, and they’re done simply by creating a script file (otherwise known as a batch file) and entering some commands. Here is how to automate this process using 7-Zip and IZArc.
Downloading and Installing 7-Zip and IZArc
You can download both of these by going to their respective download pages and choosing the option that is suited to your device and operating system.
Installing 7-zip
- Open the .exe file you downloaded from the official site.
- Choose a destination folder. The default one is C:\Program files, but you can change it by clicking on the three dots.
- Click Install.
- And when the process is finished, click Close.
Installing IZArc
- Download the IZArc Command Line Add-On.
- Open the .exe file to begin the installation.
- Click Next.
- Read (or pretend to read) the License Agreement and then select “I accept the agreement”, and click Next.
- Choose a destination folder. The default one is in C:\Program files, but you can change it by clicking on the Browse.
- Click Next.
- Click Install.
- Click Finish.
Test the Installation
This is done to make sure the command prompt can find the application. If it can’t, you need to install the app again.
- Hit the Start button and type “cmd” on your keyboard.
- Type “cd C:\Program Files\7-zip” or “cd C:\Program Files\izarc”
- Type “7z” or “izarcc”
- If it is working properly you will see something like “Usage: 7z <command>” and “Usage: izarcc <command>”.
Create or Modify Batch Files
This guide will use pre-prepared batch files. These files will compress all of the contents from the dedicated folder into a zipped archive, and include the date of compression in the archive name. It will also move the zipped archive to the storage folder, while removing the source files (optional). Zipdir will be the name of the location that contains the source files, and movedir will be the location to which the batch file will transfer the archives.
Using the Batch File with 7-zip
- Go to this link
- Copy and paste the text to Notepad.
- Name the file “test7ip.bat”. Replace “_.txt” with .bat by pressing right-click on the file and clicking Rename.
- Make two folders and name them “zipdir” and “movedir”.
- Modify the paths as instructed in the script.
- Place the files you want to compress into “zipdir”
- Use the command “7za a -tzip “%movedir%\%myfolder%_%TODAY%.zip” -r “%zipdir%\*.*” -mx5”
- “7z” begins the process of compressing the files.. “a” commands the addition of files to the compressed folder.
- “tzip”sets the compression folder type. The standard format is a zip file.
- “%movedir%\%myfolder%_%TODAY%.zip”is the name of the zipped folder.
- “%zipdir%\*.*”represents all the files in the “zipdir” folder ready for zipping. Files from all folders in “zipdir” are included in the zip folder.
- “mx5” sets the compression type to a zip mode with a compression level of five.
You can customize the batch file to suit your needs.
Using the Batch File with IZArc
- Go to this
- Copy and paste the text to Notepad.
- Name the file “testizarc.bat”. Replace “_.txt” with .bat by pressing right-click on the file and clicking Rename.
- Make two folders and name them “zipdir” and “movedir”.
- Modify the paths as instructed in the script.
- Place the files you want to compress into “zipdir”
- Use the command “izarcc -a -r -p -cx “%movedir%\%myfolder%_%TODAY%.zip” “%zipdir%\*.*””
- “Izarcc” begins the process of compressing the files.
- “a” moves files to the compressed folder.
- “r” re-applies the algorithm to all folders in zipdir.
- “p” saves the file paths so the folder retains the same arrangement.
- “cx” modifies the compression to the highest level.
- “%movedir%\%myfolder%_%TODAY%.zip” is the name of the zipped folder.
- “%zipdir%\*.*” are all the files in the “zipdir” folder ready for zipping.
That’s All There Is to It
As you can see, you don’t have to type any lines of code by yourself as there are already prepared batch files that can do the job for you. Just follow the guide exactly and you will be zipping faster than ever before!
6 thoughts on “How to Automatically Zip Files with a Batch File”
@ECHO OFF
Rem — Prepare the Command Processor
SETLOCAL EnableExtensions EnableDelayedExpansion
Title 7zip commandline – extract archive
REM Uses standalone console version of 7-Zip to extract archive to
REM a user defined folder – folder will be created if it doesn’t exist.
REM ——— Set path of console version 7-Zip (change as required) ———-
set path=”F:\Utilities\7zipCommmandline_new\”;%path%
REM ——————————————————————————————-
REM — Set location of the archive to extract (change as required) ———–
set “archive1=C:\Users\user2\test28\7zip\folder1\myfoldername_102021.zip
REM ——————————————————————————————-
REM ————- Set location to extract above archive —————————–
set “movedir1=C:\Users\user2\Desktop\test\7zip\folder1\folder12”
REM ——————————————————————————————-
REM Extract commandline. x command preserves full paths, using e command does not
ECHO.
7za x %archive1% -o%movedir1%
ECHO.
ECHO.
ECHO Archive – %archive1%
ECHO Extracted to – %movedir1%
ECHO.
ECHO.
ECHO Task Complete …
ECHO.
ECHO Press any key to exit …
pause > NUL
EXIT /B 0
Thanks for this batch file. I use 7zip and it works fine.
Is it possible to specify multiple source locations as “zipdir”? I want multiple sources to be zipped while running the batch file and then be moved to the same destination. A separate subfolder must then be created for each source location. “movedir” is therefore 1 destination containing several subfolders.
I have made the following adjustments:
set “zipdir=\\ml-nexis\project_workspace_1\Unity Attic”
set myfolder=%zipdir%
7za a -tzip “%movedir%\%myfolder%.zip” -r “%zipdir%\*.*” -mx5
The destination will then be:
\\ml-nexis\project_workspace_1\Unity Attic.zip
As a source location, I want to:
\\ml-nexis\project_workspace_1\Unity Attic
\\ml-nexis\project_workspace_2\Unity Attic
\\ml-nexis\project_workspace_3\Unity Attic
etcetera…
As a destination, I want to:
\\ml-nexis\project_workspace_1\Unity Attic.zip
\\ml-nexis\project_workspace_2\Unity Attic.zip
\\ml-nexis\project_workspace_3\Unity Attic.zip
etcetera…
Can anyone tell me how to adjust the batch file so that it works properly?
Thanks!
Glad it worked for you. I realized later that there is some redundant code, such as the three similar zip command lines:
7za a -tzip “%movedir2%\%myfolder%_%TODAY%.zip” -r “%zipdir2%\*.*” -mx5
%zipdir1%, %zipdir2%, and %zipdir2% are not required since the relevant directory was made the working directory using the pushd commands beforehand. That doesn’t affect functionality or performance, but “%zipdir2%\*.*” for example could be replaced with just “*.*”
thank you for the file.
Please can you tell me how I can unzip all file in a folder with bat file?
thanks
@ECHO OFF
Rem — Prepare the Command Processor
SETLOCAL EnableExtensions EnableDelayedExpansion
Title 7zip commandline – 3 source folders and 3 destination folders
REM Uses standalone console version of 7-Zip to Zip files in 3 user defined
REM folders and moves the zip archives to 3 user defined folders
REM ——— Set path of console version 7-Zip (change as required) ———-
set path=”F:\Utilities\7zipCommmandline_new\”;%path%
REM —————————————————————————
REM ———- Set locations of the folders to zip (change as required) ——-
set “zipdir1=C:\Users\user\Desktop\test\7zip\folder1\New Folder\
set “zipdir2=C:\Users\user\Desktop\test\7zip\folder2\New Folder\
set “zipdir3=C:\Users\user\Desktop\test\7zip\folder3\New Folder\
REM —————————————————————————
REM —– Set locations of the folders to move archive (change as required) —
set “movedir1=C:\Users\user\Desktop\test\7zip\folder1”
set “movedir2=C:\Users\user\Desktop\test\7zip\folder2”
set “movedir3=C:\Users\user\Desktop\test\7zip\folder3”
REM —————————————————————————
REM —————– Set folder name to use for archive ———————-
set myfolder=myfoldername
REM —————————————————————————
REM Replace space in hour with zero if it’s less than 10
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%
REM This sets the date like this: mm-dd-yr-hrminsecs1/100secs
Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%
REM Change working dir to %zipdir1%
pushd %zipdir1%
ECHO Zipping all contents of %zipdir1% and moving archive %myfolder% to %movedir1%
ECHO.
7za a -tzip “%movedir1%\%myfolder%_%TODAY%.zip” -r “%zipdir1%\*.*” -mx5
ECHO.
popd
REM Change working dir to %zipdir2%
pushd %zipdir2%
ECHO Zipping all contents of %zipdir2% and moving archive %myfolder% to %movedir2%
ECHO.
7za a -tzip “%movedir2%\%myfolder%_%TODAY%.zip” -r “%zipdir2%\*.*” -mx5
ECHO.
popd
REM Change working dir to %zipdir3%
pushd %zipdir3%
ECHO Zipping all contents of %zipdir3% and moving archive %myfolder% to %movedir3%
ECHO.
7za a -tzip “%movedir3%\%myfolder%_%TODAY%.zip” -r “%zipdir3%\*.*” -mx5
ECHO.
popd
REM Delete entire contents of “%zipdir%”
call:ask
ECHO.
ECHO Tasks Complete …
ECHO.
ECHO Press any key to exit …
pause > NUL
EXIT /B 0
:ask
set INPUT=
set /p INPUT=Delete al original files and dirs in “%zipdir1%” “%zipdir2%” and “%zipdir3%” ^?(y/n):
if not defined INPUT goto ask
if /i “%INPUT:”=%”==”y” goto yes
if /i “%INPUT:”=%”==”n” goto no
echo Incorrect input, try again & goto ask
:yes
echo deleting contents from “%zipdir1%” “%zipdir2%” and “%zipdir3%” …
del /q “%zipdir1%\*”
for /d %%x in (“%zipdir1%\*”) do @rd /s /q “%%x”
del /q “%zipdir2%\*”
for /d %%x in (“%zipdir2%\*”) do @rd /s /q “%%x”
del /q “%zipdir3%\*”
for /d %%x in (“%zipdir3%\*”) do @rd /s /q “%%x”
goto :eof
:no
echo.
echo No files or dirs deleted …
goto :eof
It works great. This is exactly what I wanted. Very nice that I can execute the batch file in this way. I am very grateful to you.