Question

I'm trying to setup 7zip for automated backups but I'm having trouble with output file names.

I tried using the %date% command but it just made 2 directories within my backup.

C:\Users\Desktop\Sun 11\07\2010.7z

How can I make it just log the day and month?

C:\Users\Desktop\Sun 11-07-2010.7z

Was it helpful?

Solution

Try

7z a %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.7z *.*     for (YYYY-MM-DD)

or

7z a %DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%.7z *.*     for (DD-MM-YYYY)

(*.* is the mask for the files to back up)

OTHER TIPS

You can use WMI to get your date details in a specific format. The problem with the output from date (and the %date% environment variable) is that it's very locale-specific.

If you execute:

wmic path win32_localtime get day^,month^,year^ /format:csv

you will see the output you need to process.

The following script will get you the yyyy-mm-dd format that you need (using the day of the week as the primary sort key is not a good idea):

@echo off
for /f "skip=2, tokens=2-4" delims=," %%a in ('wmic path win32_localtime get day^,month^,year^ /format:csv') do (
    set /a ymd = 10000 * %%c + 100 * %%b + %%a
)
set ymd=%ymd:~0,4%-%ymd:~4,2%%ymd:~6,2%
echo %ymd%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top