0

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!

Rachael
  • 1,905
  • 3
  • 25
  • 52

3 Answers3

0
 forfiles /d +0 /c "cmd /c echo @path"

See forfiles /?

Noodles
  • 1,951
  • 1
  • 9
  • 4
0

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.

Mofi
  • 42,677
  • 15
  • 72
  • 128
  • Thanks for providing a really thorough answer, but I don't think you read my question right. None of this really applies to any of my misunderstanding. I stated right off the bat that I was able to successfully create the zipped directory to add the existing file to using 7zip, using the date format I provided). Seeing the escape character in your `-i^!` statement (although I don't understand your placement of the escape caret) did help me get to the right answer. I can't upvote you because your answer is (in my opinion) very irrelevant. – Rachael Aug 18 '14 at 17:54
  • In all languages an escape character must be inserted left of the character to escape and not right of it, see [How can I escape an exclamation mark ! in cmd scripts?](http://stackoverflow.com/q/3288552/3074564) And I explained in detail the building of the date string because environment variable **DATE** contains obviously a completely different string on my computer as on your computer and you have not written how date string looks on your PC. However, for some unknown reason you get your command working with inserting `^` after `!` and that is the only important thing here. – Mofi Aug 19 '14 at 05:51
0

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.       
Rachael
  • 1,905
  • 3
  • 25
  • 52