Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a date in batch files

I have the following line in a batch file (that runs on an old Windows 2000 box):

7z a QuickBackup.zip *.backup 

How do I append the date to the QuickBackup.zip file. So if I ran the batch file today, ideally, the file would be QuickBackup20090514.zip.

Is there a way to do this?

like image 856
AngryHacker Avatar asked May 14 '09 17:05

AngryHacker


People also ask

How do I append to a batch file?

Content writing to files is also done with the help of the double redirection filter >>. This filter can be used to append any output to a file. Following is a simple example of how to create a file using the redirection command to append data to files.

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ).


1 Answers

Bernhard's answer needed some tweaking work for me because the %DATE% environment variable is in a different format (as commented elsewhere). Also, there was a tilde (~) missing.

Instead of:

set backupFilename=%DATE:~6,4%%DATE:~3,2%%DATE:0,2%

I had to use:

set backupFilename=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%

for the date format:

c:\Scripts>echo %DATE%

Thu 05/14/2009

like image 175
Chris Avatar answered Sep 17 '22 11:09

Chris