TechJunkie is a BOX20 Media Company

Home Cut the Cord How to Automatically Zip Files with a Batch File

How to Automatically Zip Files with a Batch File

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

  1. Open the .exe file you downloaded from the official site.
  2. Choose a destination folder. The default one is C:\Program files, but you can change it by clicking on the three dots.
  3. Click Install.
  4. And when the process is finished, click Close.

Installing IZArc

  1. Download the IZArc Command Line Add-On.
  2. Open the .exe file to begin the installation.
  3. Click Next.
  4. Read (or pretend to read) the License Agreement and then select “I accept the agreement”, and click Next.
  5. Choose a destination folder. The default one is in C:\Program files, but you can change it by clicking on the Browse.
  6. Click Next.
  7. Click Install.
  8. 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.

Path

  1. Hit the Start button and type “cmd” on your keyboard.
  2. Type “cd C:\Program Files\7-zip” or “cd C:\Program Files\izarc”
  3. Type “7z” or “izarcc”
  4. 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

7zip

  1. Go to this link
  2. Copy and paste the text to Notepad.
  3. Name the file “test7ip.bat”. Replace “_.txt” with .bat by pressing right-click on the file and clicking Rename.
  4. Make two folders and name them “zipdir” and “movedir”.
  5. Modify the paths as instructed in the script.
  6. Place the files you want to compress into “zipdir”
  7. Use the command “7za a -tzip “%movedir%\%myfolder%_%TODAY%.zip” -r “%zipdir%\*.*” -mx5”
  8. “7z” begins the process of compressing the files.. “a” commands the addition of files to the compressed folder.
  9. “tzip”sets the compression folder type. The standard format is a zip file.
  10. “%movedir%\%myfolder%_%TODAY%.zip”is the name of the zipped folder.
  11. “%zipdir%\*.*”represents all the files in the “zipdir” folder ready for zipping. Files from all folders in “zipdir” are included in the zip folder.
  12. “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

  1. Go to this
  2. Copy and paste the text to Notepad.
  3. Name the file “testizarc.bat”. Replace “_.txt” with .bat by pressing right-click on the file and clicking Rename.
  4. Make two folders and name them “zipdir” and “movedir”.
  5. Modify the paths as instructed in the script.
  6. Place the files you want to compress into “zipdir”
  7. Use the command “izarcc -a -r -p -cx “%movedir%\%myfolder%_%TODAY%.zip” “%zipdir%\*.*””
  8. “Izarcc” begins the process of compressing the files.
  9. “a” moves files to the compressed folder.
  10. “r” re-applies the algorithm to all folders in zipdir.
  11. “p” saves the file paths so the folder retains the same arrangement.
  12. “cx” modifies the compression to the highest level.
  13. “%movedir%\%myfolder%_%TODAY%.zip” is the name of the zipped folder.
  14. “%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!

How to Automatically Enable Flash

Read Next 

6 thoughts on “How to Automatically Zip Files with a Batch File”

Alex says:
Hey, any way to open add a unique password to each one?
Muhammad Rizwanullah Shaik says:
Hi. Is it possible specific extension files inside the zip folder. For example to extract .mdb files from zip files located inside a folder
tenpad says:
Extracting is even easier:

@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

Laurens says:
Hi,

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!

tenpad says:
@Laurens

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 “*.*”

Andy says:
Hi,
thank you for the file.
Please can you tell me how I can unzip all file in a folder with bat file?

thanks

tenpad says:
The easiest way to achieve that would be to strip out and simplify the code as below:

@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

Laurens says:
Thank you tenpad!

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.

Henry says:
Thank you for the file. I was able to modify it so as to name the folder with a constant name and eliminate the time (needed date only). Doesn’t sound like much, but for a well “seasoned” citizen learning theses kind of things keeps the brain cells from dying off.

Leave a Reply

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


Kerry Bayley

Nov 25, 2019

My career as a project manager has given me plenty of time to get familiar with the full Microsoft suite, as well as a host of other tools, all of which I write about online.

1952 Articles Published

More