8

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.

5 Answers 5

11
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

6
  • 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, 2010 at 20:26
  • 1
    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
    – Sathyajith Bhat
    Nov 5, 2010 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, 2010 at 5:30
  • @TMRW you're welcome. I've edited the answer to reflect what actually works.
    – Sathyajith Bhat
    Nov 8, 2010 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, 2012 at 12:14
3

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.

0

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. http://www.devguru.com/technologies/vbscript/14048.asp

4
  • I think this "should" be possible: cybernetnews.com/cybernotes-7-zip-command-line-backups .Meaning %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_Backup.7z should give a date.
    – TMRW
    Nov 4, 2010 at 0:36
  • Btw:i use Windows 7 x64
    – TMRW
    Nov 4, 2010 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 )
    – Sathyajith Bhat
    Nov 4, 2010 at 3:02
  • The only way to be sure the command will work even after locale is changed. +1.
    – noober
    Nov 15, 2012 at 12:16
0

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.

EDIT:
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
1
  • Can you paste the script here?.I will take a look and see if i can adapt it to my needs.
    – TMRW
    Nov 4, 2010 at 0:38
0

I have simpler solution :

7z a "%DATE%_%TIME:~0,2%.%TIME:~3,2%_Backup.7z" c:\test

Output:

17.03.2019_18.43_Backup.7z

3
  • 1
    mirek190 (1) You are answering a question which has been asleep for nearly 9 years. (2) It has already been shown that slicing %DATE% and %TIME% gives different results for different locale settings. Mar 17, 2019 at 20:37
  • And why are you even using %TIME%? The OP doesn’t want the filename to include the time. Mar 18, 2019 at 0:34
  • In that case completely enough is 7z a "%DATE%_Backup.7z" c:\test output will be 17.03.2019_Backup.7z
    – mirek190
    Mar 18, 2019 at 21:53

You must log in to answer this question.

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