在 Windows 7 中的命令中获取自定义日期格式

在 Windows 7 中的命令中获取自定义日期格式

我正在 Windows 7 中的 .bat 文件中创建备份脚本。作为脚本的一部分,我创建了一个日志文件。我想创建具有以下日期格式的日志文件:YYYYDDMM_HH24MISS_backup.log

关于如何做到这一点有什么想法吗?

答案1

子串的乐趣!

set filename=%date:~6,4%%date:~0,2%%date:~3,2%_%time:~0,2%24%time:~3,2%%time:~6,2%_backup.log

然后只需在脚本中将数据附加到其中:

echo "some new data" >> %filename%
ipconfig /all >> %filename%

输出文件名示例:

20092810_23242544_备份.log

不确定是否要在 HH 中的小时数后面硬编码 2424

答案2

感谢大家的帮助!根据您发布的所有信息,我最终使用了以下方法:

REM SET YEAR
set YEAR=%date:~6,4%

REM SET MONTH
set MONTH=%date:~3,2%
if %MONTH% LSS 10 set MONTH=%MONTH:~1,2%
if %MONTH% LSS 10 set MONTH=0%MONTH%

REM SET DAY
set DAY=%date:~0,2%
if %DAY% LSS 10 set DAY=%DAY:~1,2%
if %DAY% LSS 10 set DAY=0%DAY%

REM SET HOUR
set HOUR=%time:~0,2%
if %HOUR% LSS 10 set HOUR=%HOUR:~1,2%
if %HOUR% LSS 10 set HOUR=0%HOUR%

REM SET MINUTE
set MINUTE=%time:~3,2%
if %MINUTE% LSS 10 set MINUTE=%MINUTE:~1,2%
if %MINUTE% LSS 10 set MINUTE=0%MINUTE%

REM SET SECOND
set SECOND=%time:~6,2%
if %SECOND% LSS 10 set SECOND=%SECOND:~1,2%
if %SECOND% LSS 10 set SECOND=0%SECOND%

set filename=%YEAR%%MONTH%%DAY%_%HOUR%%MINUTE%%SECOND%.log

答案3

当月份或小时小于 10 时,另一个答案将会被阻塞。

REM get a date time for a logfile name
REM If the echo %time% does not return a 24 hr time on the target OS / region settings,
REM You will need to do something like 
REM    set %ampm%=%time:~9,2%
REM    if "%ampm%" EQU "PM" set /a hour=hour+12

REM testcase 1 with months, hours, mins, etc, less than 10.  
REM Make sure to test with a format matches what your OS/regional settings will produce 
REM test data should match the output from  "for /f "tokens=1,2" %%u in ('date /t') do set d=%%v"
REM and for /f "tokens=1" %%u in ('echo %time%') do set t=%%u

REM set d=01/02/2009
REM set t=3:04:05.06

REM testscase 2 with full width month, hours, mins, etc.
REM Make sure to test with a format matches what your OS/regional settings would produce in this case.

REM set d=10/20/2009
REM set t=10:20:30.40

REM if not testing, use the real date and time:
REM 
REM the next line grabs the second (space) delimited thing from 'date /t', trims up spaces, and stores it in d
for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
REM this times up the spaces from 'time'
for /f "tokens=1" %%u in ('echo %time%') do set t=%%u
if "%t:~1,1%"==":" set t=0%t%

echo d has the value: "%d%"
echo t has the value: "%t%"

REM @echo off
set hour=%t:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set min=%t:~3,2%
set secs=%t:~6,2%

set year=%d:~-4%
set month=%d:~0,2%
set day=%d:~3,2%

echo year=%year%,month=%month%,day=%day%,hour=%hour%,min=%min%,secs=%secs%

set datetimePartOfFile=%year%%day%%month%_%hour%24%min%%secs%
echo %datetimePartOfFile%

set filename=%datetimePartOfFile%_backup.log
echo %filename%

ren logfile.log %filename%

一旦您确定它可以起作用,您就可以删除其中的大部分内容,并使用少于 10 的月份和小时数以及您的区域设置进行测试。

这是精简版本,没有注释或测试用例:

再次检查它是否适合您的区域设置。

for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
for /f "tokens=1" %%u in ('echo %time%') do set t=%%u
if "%t:~1,1%"==":" set t=0%t%
set hour=%t:~0,2%
set min=%t:~3,2%
set secs=%t:~6,2%
set year=%d:~-4%
set month=%d:~0,2%
set day=%d:~3,2%
set datetimePartOfFile=%year%%day%%month%_%hour%24%min%%secs%
set filename=%datetimePartOfFile%_backup.log
ren logfile.log %filename%

答案4

@echo off

set fileName=Nome do seu arquivo

set extension=xls

set destination=C:/Users/Chunda/Dropbox

set ano=%date:~6,4%
set mes=%date:~3,2%
set dia=%date:~0,2%

set hora=%time:~0,2%
set minuto=%time:~3,2%
set segundo=%time:~6,2%

set backupName=_%ano%-%mes%-%dia%_%hora%h%minuto%m%segundo%s

set commandString="%fileName%.%extension%" "%destination%%fileName%%backupName%.%extension%"

copy %commandString%

echo %fileName%.%extension%

pause

相关内容