72231

Use 7zip to include files with *current date* only

Question:

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!

Answer1:

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

See forfiles /?

Answer2:

Format of date string of environment variable <strong>DATE</strong> 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 <strong>DATE</strong> four characters beginning from seventh character. First character has character index 0.

%DATE:~3,2% ... extracts from string of environment variable <strong>DATE</strong> two characters beginning from fourth character.

%DATE:~0,2% ... extracts from string of environment variable <strong>DATE</strong> 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 <strong>DATE</strong>.

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.

Answer3:

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.

Recommend

  • In Akka, is ActorContext thread safe?
  • How to use the resource module to measure the running time of a function?
  • How do you keep a running instance for Google App Engine
  • How to skip require in ruby?
  • Saving image to sd with current date and time in name doesn't work
  • Hide HTML elements without javascript, only CSS
  • How to access culture data in globalize.js V1.0.0
  • NUnit 3.0 TestCase const custom object arguments
  • List images(01.png) and descriptions(01.txt) from directory
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Code in Job's Script Block after Start-Process Does not Execute
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • OOP Javascript - Is “get property” method necessary?
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to get Eclipse Oxygen to run on Java 9
  • Android activity accessing service's static reference before the service is ready
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Yii2: Config params vs. const/define
  • Switching to Release Build causes runtime error in Web Reference
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Illegal mix of collations for operation for date/time comparison
  • C# - Serializing and deserializing static member
  • Sony Xperia Z Tablet not found by adb
  • javascript inside java/jsp code
  • Javascript convert timezone issue
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • XCode can't find symbols for a specific iOS library/framework project
  • R: gsub and capture
  • 0x202A in filename: Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • How do you join a server to an Active Directory (domain)?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • How to Embed XSL into XML
  • Net Present Value in Excel for Grouped Recurring CF