2

I am trying to set up a 7zip batch file to backup data in an archive with the current date in the file name.

So far I set up

@ECHO OFF
7z a C:\Download\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_Backupfile.7z -mhe -mx0 -r -ssw -w -i@files_included_in_backup.txt -x@files_excluded.txt

pause

The backup itself goes fine, and it should result in a file named JJJ-MM-DD_Backupfile.7z.

Though today on Aug 13 it gives me a file named 2014-8. -01_Backupfile.7z

any idea what´s going wrong? I am using Windows 8.1 64bit and 7-Zip command line version 9.20.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
Roger23K
  • 21
  • 1
  • 2
  • 1
    From a cmd prompt try this: `echo "%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%"` and paste the result here. – foxidrive Aug 13 '14 at 10:11
  • hi, doing so results in: 2014-8. -01 – Roger23K Aug 13 '14 at 10:27
  • can you paste the result of `echo "%date%"` The problem is that `%date%` is variable from computer to computer, depending on settings and country. I'll post a robust method in an answer. – foxidrive Aug 13 '14 at 11:55

1 Answers1

1

Give this a shot:

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

7z a "C:\Download\%datestamp%_Backupfile.7z" -mhe -mx0 -r -ssw -w -i@files_included_in_backup.txt -x@files_excluded.txt
foxidrive
  • 40,353
  • 10
  • 53
  • 68