7-Zip CMD: Add current date to archive and include only the last modified folder in archive
I'm trying to create a backup script with 7-Zip. I have everything else done already, but these two issues are still blocking me.
Firstly, I want to add a timestamp with a create date like this: 3.11.2010 Backup.7z
So the dd.mm.yyyy
format. I tried several versions I found on the Internnet, but none of these would work the way I wanted. What should I do?
Secondly, I only want to add the last modified folder to the archive (no matter how many folders exist in directory; I just need the latest). Basically, I have something like this:
App_v.1.0.0.4.exe_Url_2um2yok5q4vpoxnvnscpq3adfwff4wsmi
App_v.1.0.0.5.exe_Url_ft4mnvbu54hfrgdhxrahj4imlmermdsoe
So I want to add the newest (1.0.0.5) folder only. These folders change and always have different names so name based sorting would be a bad idea.
Solution 1:
7z a -r "%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4% Backup".7z
Will create the archive with DD.MM.YYYY Backup.7z
Format.
Explanation: Echoing %DATE%
prints the date in your regional date format setting.
D:\>echo %DATE%
Thu 11/04/2010
By using ~x,y
specifier your are doing an in string/substring extraction of the string - where x
is the starting character and y
number of characters you wish to extract.
On your second point:
I only want to add last modified folder to archive(no matter how many folders exist in directory I just need the latest).
7z u -r "%DATE:~2,2%.%DATE:~5,2%.%DATE:~-4% Backup".7z
should do it.
Solution 2:
You can use "windows management instrumentation". See this one
test1.bat
:
set mydate=
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined mydate set mydate=%%x
echo Current date is %mydate%
Result:
Current date is 20140202145547.557000+060
Regardless of your "regional settings" (date format) in "Control Panel" it always returns YYYYMMDD.