Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-02-18
5
Medium Priority
?
1,095 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 .
0
Comment
Question by:htetzaw
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:GuruGary
ID: 23678076
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

0
 

Author Comment

by:htetzaw
ID: 23678782
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.
0
 
LVL 10

Accepted Solution

by:
GuruGary earned 320 total points
ID: 23678915
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

0
 

Author Comment

by:htetzaw
ID: 23688523
Thanks Gary! I will test it tonite and will let you know the result !
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Steps to fix “Unable to mount database. (hr=0x80004005, ec=1108)”.
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

578 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