The format of the date string of the dynamic variable DATE depends on which country/region is configured for the used account.
Executing a batch file with the two lines below on German Windows XP with Germany or Austria configured as country for the used account
@echo %DATE%
@echo %DATE:~6,4%-%DATE:~5,2%-%DATE:~8,2%
results in the output
16.08.2014
-.2-14
This is not a valid date string in format YYYY-MM-DD
as it is wanted obviously in the command line.
I needed to change the second line to
@echo %DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
to get output on running the batch file in a command prompt window
16.08.2014
2014-08-16
Explanation for above date string with substring extraction:
%DATE:~6,4%
... extracts from string of dynamic variable DATE four characters beginning from seventh character. First character has character index 0.
%DATE:~3,2%
... extracts from string of dynamic variable DATE two characters beginning from fourth character.
%DATE:~0,2%
... extracts from string of dynamic variable DATE two characters beginning from first character.
Now is known what the date substring extraction code in the commands do. There can be seen also how to verify the output of the substring extraction code using a small batch file executed from within a command prompt window, or from Windows Explorer after appending a third line with command pause
to see the output.
This should help finding the correct code for date string building on the computer according to requested date format depending on date string format of dynamic variable DATE.
The character !
has a special meaning in batch files as it is used for referencing the value of an environment or dynamic variable on enabled delayed expansion. It must be escaped with two ^
which means putting left to !
twice the character ^
resulting in ^^!
for getting it interpreted as literal character in a batch file on delayed expansion being enabled somewhere above the command line. The exclamation mark can be written in the batch file just with !
on disabled delayed expansion as by default. The command line itself does not require enabled delayed variable expansion.
The command
7z.exe a -tzip C:\RACHAEL\my_work\dbs\MyDb\%DATE:~11,4%\%DATE:~5,2%\MyDb_bak_%DATE:~11,4%-%DATE:~5,2%-%DATE:~8,2%.bak.zip -i^^!C:\RACHAEL\my_work\dbs\MyDb_bak_%DATE:~11,4%-%DATE:~5,2%-%DATE:~8,2%.bak
might be therefore the right one in a batch file on your computer.
But on German Windows XP with Germany or Austria configured as country for the used account the right command is:
7z.exe a -tzip C:\RACHAEL\my_work\dbs\MyDb\%DATE:~6,4%\%DATE:~3,2%\MyDb_bak_%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%.bak.zip -i^^!C:\RACHAEL\my_work\dbs\MyDb_bak_%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%.bak
In batch file it is often better to specify executables with full path. Therefore it is better here to specify not just 7x.exe
, but something like "C:\Program Files\7-Zip\7z.exe"
. The path to 7x.exe
may be different on your computer.
The usage of the dynamic variable DATE is very fast, but a country/region independent solution would be better for a batch file which should run on any Windows computer independent on which country/region is configured for the used account. See my answer on time is set incorrectly after midnight which explains in full details two methods to get current date region independent.
Here is a solution using ROBOCOPY to get day in month, month and year.
for /F "tokens=1-3 delims=/ " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "CurrentDate=%%G-%%H-%%I" & set "CurrentYear=%%G" & set "CurrentMonth=%%H" & goto CreateBackup
:CreateBackup
"C:\Program Files\7-Zip\7z.exe" a -tzip C:\RACHAEL\my_work\dbs\MyDb\%CurrentYear%\%CurrentMonth%\MyDb_bak_%CurrentDate%.bak.zip -i^^!C:\RACHAEL\my_work\dbs\MyDb_bak_%CurrentDate%.bak