windows - Batch command date and time in file name -
i compressing files using winzip on command line. since archive on daily basis, trying add date , time these files new 1 auto generated every time.
i use following generate file name. copy paste command line , should see filename date , time component.
echo archive_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.zip
output
archive_20111011_ 93609.zip
however, issue am vs pm. time stamp gives me time 9
(with leading blank space) vs. 10
naturally taking 2 spaces.
i guess issue extend first 9 days, first 9 months, etc. well.
how fix leading zeroes included instead of leading blank spaces archive_20111011_093609.zip
?
another solution:
for /f "tokens=2 delims==" %%i in ('wmic os localdatetime /format:list') set datetime=%%i
it give (independent of locale settings!):
20130802203023.304000+120 ( yyyymmddhhmmss.<milliseconds><always 000>+/-<timedifference utc> )
from here, easy:
set datetime=%datetime:~0,8%-%datetime:~8,6% 20130802-203023
for logan's request same outputformat "date-time modified" of file:
for %%f in (test.txt) set file=%%~ff /f "tokens=2 delims==" %%i in ('wmic datafile name^="%file:\=\\%" lastmodified /format:list') set datetime=%%i echo %datetime%
it bit more complicated, because works full paths, wmic
expects backslashes doubled , =
has escaped (the first one. second 1 protected surrounding quotes).
Comments
Post a Comment