开发者

How can I output weekday and month in batch (log files)?

开发者 https://www.devze.com 2023-01-23 18:45 出处:网络
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 m开发者_开发知识库ade 2 directories within my backup.

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 m开发者_开发知识库ade 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


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)


Are you using a bat-file? Look here http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/


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%
0

精彩评论

暂无评论...
验证码 换一张
取 消