更新:现在是2016年,我将使用PowerShell,除非有一个真正令人信服的向后兼容的原因,特别是因为使用日期的区域设置问题。参见@npocmaka的https://stackoverflow.com/a/19799236/8479
什么是Windows命令行语句(s),我可以使用以我可以放入文件名的格式获取当前日期时间?
我想要一个.bat文件,它将一个目录压缩成一个归档文件,其中当前日期和时间作为名称的一部分,例如Code_2008-10-14_2257.zip。有什么简单的方法可以做到这一点,独立于机器的区域设置吗?
我真的不介意日期格式,理想情况下它是yyyy-mm-dd,但任何简单的都可以。
到目前为止,我已经得到了这个,在我的机器上给我Tue_10_14_2008_230050_91:
rem Get the datetime in a format that can go in a filename.
set _my_datetime=%date%_%time%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_%
rem Now use the timestamp by in a new ZIP file name.
"d:\Program Files\7-Zip\7z.exe" a -r Code_%_my_datetime%.zip Code
我可以接受这个,但它似乎有点笨重。理想情况下,它应该更简短,并具有前面提到的格式。
我用的是Windows Server 2003和Windows XP Professional。我不想安装额外的实用程序来实现这一点(尽管我知道有一些工具可以很好地格式化日期)。
给定一个已知位置,供函数形式参考。ECHOTIMESTAMP调用显示了如何将时间戳转换为变量(本例中为DTS)。
@ECHO off
CALL :ECHOTIMESTAMP
GOTO END
:TIMESTAMP
SETLOCAL EnableDelayedExpansion
SET DATESTAMP=!DATE:~10,4!-!DATE:~4,2!-!DATE:~7,2!
SET TIMESTAMP=!TIME:~0,2!-!TIME:~3,2!-!TIME:~6,2!
SET DTS=!DATESTAMP: =0!-!TIMESTAMP: =0!
ENDLOCAL & SET "%~1=%DTS%"
GOTO :EOF
:ECHOTIMESTAMP
SETLOCAL
CALL :TIMESTAMP DTS
ECHO %DTS%
ENDLOCAL
GOTO :EOF
:END
EXIT /b 0
并保存到文件,timestamp.bat,下面是输出:
:: GetDate.cmd -> Uses WMIC.exe to get current date and time in ISO 8601 format
:: - Sets environment variables %_isotime% and %_now% to current time
:: - On failure, clears these environment variables
:: Inspired on -> https://ss64.com/nt/syntax-getdate.html
:: - (cX) 2017 adolfo.dimare@gmail.com
:: - http://stackoverflow.com/questions/203090
@echo off
set _isotime=
set _now=
:: Check that WMIC.exe is available
WMIC.exe Alias /? >NUL 2>&1 || goto _WMIC_MISSING_
if not (%1)==() goto _help
SetLocal EnableDelayedExpansion
:: Use WMIC.exe to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC.exe Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto _WMIC_done_
set _yyyy=%%L
set _mm=00%%J
set _dd=00%%G
set _hour=00%%H
set _minute=00%%I
set _second=00%%K
)
:_WMIC_done_
:: 1 2 3 4 5 6
:: %%G %%H %%I %%J %%K %%L
:: Day Hour Minute Month Second Year
:: 27 9 35 4 38 2017
:: Remove excess leading zeroes
set _mm=%_mm:~-2%
set _dd=%_dd:~-2%
set _hour=%_hour:~-2%
set _minute=%_minute:~-2%
set _second=%_second:~-2%
:: Syntax -> %variable:~num_chars_to_skip,num_chars_to_keep%
:: Set date/time in ISO 8601 format:
Set _isotime=%_yyyy%-%_mm%-%_dd%T%_hour%:%_minute%:%_second%
:: -> http://google.com/search?num=100&q=ISO+8601+format
if 1%_hour% LSS 112 set _now=%_isotime:~0,10% %_hour%:%_minute%:%_second%am
if 1%_hour% LSS 112 goto _skip_12_
set /a _hour=1%_hour%-12
set _hour=%_hour:~-2%
set _now=%_isotime:~0,10% %_hour%:%_minute%:%_second%pm
:: -> https://ss64.com/nt/if.html
:: -> http://google.com/search?num=100&q=SetLocal+EndLocal+Windows
:: 'if () else ()' will NOT set %_now% correctly !?
:_skip_12_
EndLocal & set _isotime=%_isotime% & set _now=%_now%
goto _out
:_WMIC_MISSING_
echo.
echo WMIC.exe command not available
echo - WMIC.exe needs Administrator privileges to run in Windows
echo - Usually the path to WMIC.exe is "%windir%\System32\wbem\WMIC.exe"
:_help
echo.
echo GetDate.cmd: Uses WMIC.exe to get current date and time in ISO 8601 format
echo.
echo %%_now%% environment variable set to current date and time
echo %%_isotime%% environment variable to current time in ISO format
echo set _today=%%_isotime:~0,10%%
echo.
:_out
:: EOF: GetDate.cmd