Thursday, 22 December 2011

Timestamps in Filenames when Using DOS Batch


To get a nicely formatted timestamp in a filename e.g. ftpTransfer_20111214@0134.txt when using DOS batch scripts to control execution you can use the following.




@ECHO OFF
::GetDate
for /f "tokens=1-3 delims=/" %%a in ('DATE/T') do (
set dd=%%a
set mm=%%b
set yyyy=%%c
)


Call :TRIMYEAR %yyyy%
SET odate=%yyyy%_%mm%_%dd%
::Get Time
SET WFLWDATE=%odate%


for /f "tokens=1-3 delims=:" %%a in ('time/t') do (
  set hh=%%a
  set mm=%%b
  set ss=%%c
)


SET WFLWTIME=%hh%%mm%


::Create TimeStamp
SET TMSTMP=%TMSTMP%@%hh%%mm%


::Do your stuff here

:TRIMYEAR
set yyyy=%*
GOTO :EOF