I'm creating then zipping a file that is stamped with the current date with 7-zip. I am able to use the following switch to create the zip file to add to:
7z -tzi 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
but adding the "-i!" command does not locate the file specfied
7z -tzi 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_<get_current_date_in_correct_format>.bak
How would one achieve locating the file in this directory containing the current formatted date from a batch file? Is there an escape character? The '%' does not provide this purpose in 7zip, which I assumed it would.
Thanks in advance!
forfiles /d +0 /c "cmd /c echo @path"
See forfiles /?
Format of date string of environment variable DATE depends on language settings of Windows.
Executing a batch file with the 2 lines below on German Windows XP
@echo %DATE%
@echo %DATE:~6,4%-%DATE:~5,2%-%DATE:~8,2%
results in output
16.08.2014
-.2-14
This is not a valid date string in format YYYY-MM-DD as you want obviously in your command.
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 environment variable DATE four characters beginning from seventh character. First character has character index 0.
%DATE:~3,2%
... extracts from string of environment variable DATE two characters beginning from fourth character.
%DATE:~0,2%
... extracts from string of environment variable DATE two characters beginning from first character.
Now you know what the date substring extraction code in your commands do. And you can see also how to verify output of 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 you to find the correct code for date string building on your computer according to requested date format depending on date string format of environment variable DATE.
Character !
has a special meaning in batch files as it is used for referencing the value of an environment variable with delayed expansion. To get it interpreted as literal character in a batch file, it must be escaped with ^
which means putting left to !
the character ^
resulting in ^!
.
Therefore 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 the right one in a batch file on your computer.
But on German Windows XP 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
One laste note: 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.
was missing the escape character ^
before the path I was trying to add to the successfully zipped directory created with 7zip.
This is the corrected statement:
echo Using 7-zip to compress today's backup folder...
7z 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\%DATE:~11,4%\%DATE:~5,2%\MyDb_bak_%DATE:~11,4%-%DATE:~5,2%-%DATE:~8,2%.bak
^missing escape character right here so that path could actually be parsed per date format.