Link to home
Start Free TrialLog in
Avatar of htetzaw
htetzaw

asked on

Create batch file to rename current folder, create new folder and zip the old folder and delete away if successful

Hi All ,I need help to create batch file to rename current folder, create new folder and zip the old folder and delete away if successful . For e.g. One application is using this folder D:/SMEX/spammail folder and I will like to rename that folder , zip it with current date, create the new folder with same name (spammail) and delete the old folder if successfully zipped .Is there any way to use the batch file for above process . Thanks in advanced ! I am going to use it on window 2000 server .
Avatar of GuruGary
GuruGary
Flag of United States of America image

Yes.  The ZIP part will depend on which zip archiver program you have installed, and the date creation will depend on what date format you want and how your computer's regional settings are configured.  I will assume to following:
* You want an alphabetical date format of YYYYMMDD (20090218)
* The Windows 2000 server is configured for US English
* You are using 7Zip as your zip archiver (my personal favorite) installed in C:\Program Files\7-Zip

Try something like this:
@echo off
setlocal
set TargetPath="D:/SMEX"
set TargetDir="spammail"
set Zipper="C:\Program Files\7-Zip\7z.exe"
set Today=%date:~-4%%date:~-10,2%%date:~-7,2%
 
ren %TargetPath%\%TargetDir% %TargetDir%_%Today%
if %errorlevel% NEQ 0 goto Error
md %TargetPath%\%TargetDir%
%Zipper% a %TargetDir%_%Today%.zip %TargetPath%\%TargetDir%_%Today%
if %errorlevel% EQU 0 rd /q /s %TargetPath%\%TargetDir%_%Today%
goto :EOF
 
:Error
echo Error renaming directory ... aborting

Open in new window

Avatar of htetzaw
htetzaw

ASKER

Hi Gary, Thanks . The script is working but is there any way to generate the error msg and cancel everything if zipping fail and where should I see the error msg . Thanks.
ASKER CERTIFIED SOLUTION
Avatar of GuruGary
GuruGary
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of htetzaw

ASKER

Thanks Gary! I will test it tonite and will let you know the result !