Automatically zip files with batch file

Posted on 2009-05-13
Last Modified: 2013-12-01
Hi Experts,

I'm trying to make a batch script that automatically zips files and deletes them after worths. Because 7-zip is a powerful tool and easy to use in the command line, i have chosen to use it.

The problem is as following:
There is an input directory(Figure 1) that have subdirectories( subdirectories or numbered 1, 2, 3, 4, etc (Figure 2)). In these directories there are files (Figure 3). All these files need to be zipped one by one with a high compression ratio and deleted after worths. The deletion needs to take place right after the file has been zipped.

My idea was make a batch file with a for loop to zip and delete the file.

Please help me out here!

Question by:DonOetje
  • 2
  • 2
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24372804
See here for learning how to use 7-zip in a batch file
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24372815
LVL 68

Accepted Solution

Qlemo earned 50 total points
ID: 24373213
Too bad 7zip doesn't allow for move ... We have to do it manually. I used the above recompression tutorial to construct a more simple batch for your requirements. The && is for preventing deletion after zipping error.
This batch will zip ANY pdf under the D:\opleveringen folder.

@echo off

for /F "tokens=*" %%F in ('dir /s/b D:\Opleveringen\*.pdf') do (

  "C:\Program Files\7-Zip\7z" a -t7z "%%~dpnF.7z" "%%F" -mx=9 && del "%%F"


Open in new window


Author Comment

ID: 24378692
Thanks for you input. I made its with the information you gave me.

I still have one problem. I made some test directories with 10 files in the directory (Figure 1). But when i run this script i only get 4 files (Figure 2), with all 10 files(Figure 3). How can i tune this script so it will zip all file apart of from the others?
@ECHO off

REM *Set options to control the visibility of environment variables in a batch file.


REM *Provide a source, destinaion and the path where to locate 7-zip

SET _source="C:\Test1"

SET _dest="C:\Test1"

SET _wrpath=C:\Program Files\7-Zip

REM *Check if the destination directory exist. If not the script will make it 

IF NOT EXIST %_dest% MD %_dest%

REM *Zip every file from the source directory and place the zip file in the destination directory, after worths delete the source file

FOR %%I IN (%_source%\*) DO (

	"%_wrpath%\7z" a -t7z "%_dest%\%%~nI.7z" "%%I" -mx=9

	REM && DEL "%%I" /I




Open in new window

LVL 68

Expert Comment

ID: 24379077
That is because the source files have an extension .pdf_*, and the file name is the same.
Change line 16 to maintain the file extension for the ZIP file.

"%_wrpath%\7z" a -t7z "%_dest%\%%~nxI.7z" "%%I" -mx=9

Open in new window


Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to run the DNS query from the server? 5 66
Moving RDP Server to New Server. 3 47
Connecting two servers 30 76
server DNS address could not be found 22 131
A while back when OPSMGR 2012 was released we were very excited about getting it into our environment and upgrading our 2007 implementation,  we started our planning and we then proceeded with our implementation. All went as planned & our system …
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now