Batch files and dates


J

Jan Krumsiek

Hi all.

I am currently writing a small batch script which shall automate my
backups. I am using the WinZip command line tool for compression and
archiving. The lines look like this:

"C:\Programme\WinZip\WZZIP.EXE" -a -P -r f:\backup.zip "d:\Database\"
"C:\Programme\WinZip\WZZIP.EXE" -a -P -r f:\backup.zip "d:\MyProjects\"

etc.

Now I'd like the destination file name to contain the current date. It
shall look like this:

f:\backup091103.zip

Are there any macros or other tools which I can use in batch files?

Regards,
Jan
 
Ad

Advertisements

T

Tim Hall

Jan,

I do the same thing as a secondary backup for our main databases i found
this nifty line that does the trick for dates, im still not 100% certain
how/why it works (as i havent bothered to look at it in much detail) but it
does for me and thats all i need to know.

for /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do set cdate=%%c-%%b-%%a

it will set the variable cdate to a YYYY-MM-DD format for me (being in
Australia and having DD-MM-YYYY format dates) you could try that see what it
gives you then tinker with the %%a %%b and %%c till you get the correct
format your after based on your date format.

then you can use soemthing like
"C:\Programme\WinZip\WZZIP.EXE" -a -P -r f:\backup%cdate%.zip "d:\Database\"


This is a full sample of my batch file, i copy the files across first then
zip up the resulting dir and remove it, this gives me control of what i want
to exclude (and i added the zipping later on as a hack job to get the size
down from 3+gb a night), i end up with a zip file called z:\nightly
backups\2003-10-31.zip.

@echo off
REM Set date variable
for /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do set cdate=%%c-%%b-%%a
REM Set backup path
set bup=z:\Nightl~1\

REM Place xopy commands below here
@echo on

xcopy k:\cds\v0\000000.* %bup%%cdate%\cds\v0\ /E /V /F /H /Y /Z
/EXCLUDE:z:\exclude.txt
xcopy k:\cds\v0\*.act %bup%%cdate%\cds\v0\ /E /V /F /H /Y /Z
/EXCLUDE:z:\exclude.txt
xcopy k:\cds\group0\*.* %bup%%cdate%\cds\group0\ /E /V /F /H /Y /Z
/EXCLUDE:z:\exclude.txt

xcopy k:\cds\tax1999\*.* %bup%%cdate%\tax1999\ /E /V /F /H /Y /Z
/EXCLUDE:z:\exclude.txt
xcopy k:\cds\tax2000\*.* %bup%%cdate%\tax2000\ /E /V /F /H /Y /Z
/EXCLUDE:z:\exclude.txt
xcopy k:\cds\tax2001\*.* %bup%%cdate%\tax2001\ /E /V /F /H /Y /Z
/EXCLUDE:z:\exclude.txt
xcopy k:\cds\tax2002\*.* %bup%%cdate%\tax2002\ /E /V /F /H /Y /Z
/EXCLUDE:z:\exclude.txt

xcopy p:\*.* %bup%%cdate%\PremierData\ /E /V /F /H /Y /Z

xcopy \\fileserver\c$\mysql\data\*.* %bup%%cdate%\mysqldata\ /E /V /F /H /Y
/Z


z:\zip.exe %bup%%cdate% -r -9 -S %bup%%cdate%

@echo off
REM Place xcopy commands above here

Rem delete temp copy dir
@echo on
rmdir %bup%%cdate% /S /Q

@echo off
REM END OF BATCH FILE


I hope that helps you.

Tim
 
J

Jan Krumsiek

for /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do set cdate=%%c-%%b-%%a

it will set the variable cdate to a YYYY-MM-DD format for me (being in
Australia and having DD-MM-YYYY format dates) you could try that see what it
gives you then tinker with the %%a %%b and %%c till you get the correct
format your after based on your date format.

then you can use soemthing like
"C:\Programme\WinZip\WZZIP.EXE" -a -P -r f:\backup%cdate%.zip "d:\Database\"
Thank you very much!
Worked great for me!

Jan
 
Ad

Advertisements

Joined
Aug 16, 2007
Messages
1
Reaction score
0
Hi,


I have a folder call C:\exportdir and there are many sub folders in there. I would like to create a batch file that would zip up each subfolder in a zip file and the name would be the same as the folder. How can I acheive this?

Thanks
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top