Software >> OS >> Windows >> Command Line >> How to create files with appended date and time

example copy of files to new filename with date and time appended for /f "tokens=2-4 delims=/ " %%a in ('Date /T') do set DateVar=%%c-%%a-%%b for /f "tokens=1-3 delims=: " %%a in ('TIME /T') do set TimeVar=%%a%%b%%c REM REM Note the delims is "/ " is either forward-slash "/" or " " space. Either one will be treated REM as a delimiter REM set backupfile=sirsi-problem-tracking-%DateVar%-%TimeVar%.doc REM echo "__%DateVar%__" REM echo "__%TimeVar%__" echo %backupfile% copy ..\sirsi-problem-tracking.doc %backupfile% *** IMPORTANT *** make sure no spaces at the end of %%b and %%c in the set statements for DateVar and TimeVar ALTERNATIVE WAY to obtain date time variables @ECHO OFF SET HH=%time:~0,2% REM SET MM=%time:~3,2% REM SET SS=%time:~6,2% REM SET Day=%date:~-10,2% REM SET Mth=%date:~-7,2% REM SET Yr=%date:~-4% SET dtStamp9=%date:~-4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2% SET dtStamp24=%date:~-4%%date:~4,2%%date:~7,2%-%time:~0,2%00 if "%HH:~0,1%" == " " (SET dtStamp=%dtStamp9%) else (SET dtStamp=%dtStamp24%)