Solved

Automatically zip files with batch file

Posted on 2009-05-13
5
1,225 Views
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!

 
figure-1.JPG
figure-2.JPG
figure-3.JPG
0
Comment
Question by:DonOetje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

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

http://aarmstrong.org/tutorials/mass-zip-rar-to-7zip-recompression-batch-file
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24372815
0
 
LVL 70

Accepted Solution

by:
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

0
 

Author Comment

by:DonOetje
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.
SETlocal
 
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
)
	
PAUSE

Open in new window

figure-1.JPG
figure-2.JPG
figure-3.JPG
0
 
LVL 70

Expert Comment

by:Qlemo
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

0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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