We help IT Professionals succeed at work.

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

htetzaw
htetzaw asked
on
Medium Priority
1,106 Views
Last Modified: 2012-05-06
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 .
Comment
Watch Question

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

Author

Commented:
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.
Commented:
Yes.  The script is already checking for an errorlevel on the ZIP process, so we can just add more logic to that part of it, and another error statement:
@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 ErrorRen
md %TargetPath%\%TargetDir%
%Zipper% a %TargetDir%_%Today%.zip %TargetPath%\%TargetDir%_%Today%
if %errorlevel% NEQ 0 goto ErrorZip
rd /q /s %TargetPath%\%TargetDir%_%Today%
goto :EOF
 
:ErrorRen
echo Error renaming directory ... aborting
goto :EOF
 
:ErrorZip
echo Error zipping files ... aborting

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Gary! I will test it tonite and will let you know the result !
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.