Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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:


So I want to add the newest ( folder only. These folders change and always have different names so name based sorting would be a bad idea.

share|improve this question
up vote 4 down vote accepted
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.

7zip date

share|improve this answer
Go figure."%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4% Backup".7z gives me .2..1.2010 Backup.7z .very odd.%DATE% shows "R 05.11.2010" .The letter is first letter of Friday in my local language. – TMRW Nov 5 '10 at 20:26
change "%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4% Backup" to "%DATE:~2,2%.%DATE:~5,2%.%DATE:~-4% Backup" - it'll work fine. @TMRW – Sathya Nov 5 '10 at 21:05
Exellent.It finally worked."%DATE:~2,2%.%DATE:~5,2%.%DATE:~-4%" did the trick.Thank you very much.Since i can´t flag this comment as accepted answer i´ll mark your second reply as answer. – TMRW Nov 8 '10 at 5:30
@TMRW you're welcome. I've edited the answer to reflect what actually works. – Sathya Nov 8 '10 at 12:51
OK. Then you change your locale and this stops working. Don't rely on string operations. That's why I'm still using WinRAR though it's less efficient. – noober Nov 15 '12 at 12:14

7zip command line isn't going to have the logic to perform the tasks you need. If you're running Linux, you'll have to figure out how to do this in your shell script. If you're in windows you can experiment with DOS batch language (which MAY be powerful enough), powershell (which requires that you install something), or write a vbscript (.vbs) file which should be a pretty good solution.

If you decide to go the vbs route, I recommend you look into using the filesystemobject with vbscript.

share|improve this answer
I think this "should" be possible: .Meaning %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_Backup.7z should give a date. – TMRW Nov 4 '10 at 0:36
Btw:i use Windows 7 x64 – TMRW Nov 4 '10 at 0:39
VBS isn't really needed, a simple Windows batch file will do. I have a backup script at work, I'll post it tomorrow morning ( my time ~ 12 hrs from now ) – Sathya Nov 4 '10 at 3:02
The only way to be sure the command will work even after locale is changed. +1. – noober Nov 15 '12 at 12:16

What I've done to get files that changed that day was use Robocopy to copy the newly modified files to a temporary directory and then 7zip that.

Right from the go I want to say I'm not batch script ninja, but through reading websites I was able to kluge something together that seem to work good enough. I'm not a programmer, but I'm the "computer guy", so I just know more than most I work with.

Here is the script I use. The date format is different than you asked for, chose YYYY-MM-DD because then things would sort well in Windows Explorer. You should be able to change that to how you want things labeled.

The /maxage:1 part of the script is what limits to things what had change within a day. I run the backup script daily.

One problem I hadn't fixed in the script is if no files have changed, a 7z file of 32k in size is still created (an empty 7z archive). I had planned to add empty directory detection, or if nothing else do a hash and compare to an empty 7z archive hash and if the match then delete the newly created empty 7z archive.

In my full backup script, I have the script hash the archive so I can manually compare the hashes of the last two full backups to see if they are the same. (Having a script like this won't detect if files were deleted because incremental backups would show no files added.) If the current hash and previous hash are the same, I delete newest backup because nothing had changed since then. (I planned to automate is checking if I had time.)

I hope cleaning the script up (removing server names and so on) I didn't break it, and I never had much time to polish up the script so it is crude. At any rate, I hope it will be good enough to get started down the right direction to a script that will meet your requirements.

If you need any clarification on switches or why I chose the switches I did, feel free to ask.

rem Setup Variables...
set zcmd="c:\program files\7-zip\7z" a -t7z -r -mx=7 -ssw -mtc=on
set backupdir=C:\_backups\2burn
set tempdir=C:\_backups\temp
rem date format YYYY_MM_DD
set mydate=%date:~10,4%_%date:~4,2%_%date:~7,2%
set rc=c:\robocopy.exe

rem Make Directories...
md %backupdir%\%mydate%
if exist %tempdir% rd %tempdir% /s /q
if not exist %tempdir% md %tempdir%

rem Copy changed files...
%rc% \\server1\source\path %tempdir%\temp\path *.* /s /z /copy:dat /maxage:1 /r:5 /w:5     /log:%backupdir%\%mydate%\log.txt /np

rem Compress changed files...
if exist %tempdir%\temp\path %zcmd% %backupdir%\%mydate%\backup1.7z %tempdir%\temp\path\*.*

rem Remove temp directory...
rd %tempdir%\temp\path\ /q /s

rem copy compressed file and log to offsite server...
%rc% /copy:dat /e /z /r:5 /w:5 %backupdir%\%mydate%  "\\remote_server\share\backup_path\%mydate%"

rem email changed files log from offsite server...
rem C:\blat\blat262\full\blat -subject "BATCH: backup1 inc backup for %mydate%." -bodyf "\\remote_server\share\backup_path\%mydate%\log.txt" -server \\mailserver -f AccountToSendTo -tf c:\ListOfAdmins2Email.txt -u Username -pw Pa$$w0rd
share|improve this answer
Can you paste the script here?.I will take a look and see if i can adapt it to my needs. – TMRW Nov 4 '10 at 0:38

You can use "windows management instrumentation". See this one


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%


Current date is 20140202145547.557000+060

Regardless of your "regional settings" (date format) in "Control Panel" it always returns YYYYMMDD.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .