Create a file with the name as the current date (windows batch)Tag(s): Misc Prog HowTo
Create a file with the current date as filename (ex. 2008-11-08.dat)
echo hello > %date%.dat
echo hello > %date:-=%.dat
echo hello > %date:-=%%time::,=%
set x=%date:-=%%time::,=%.dat set x=%x:,=% echo hello > %x%
@echo off setlocal ENABLEDELAYEDEXPANSION set today=!date:/=-! set now=!time::=-! set millis=!now:*.=! set now=!now:.%millis%=! echo hello > log-!today!_!now!.txt
One way to eliminate the risk is to use WMIC to extract the date information and assign the value into our own environment variable and use to name our log file (ex. log-2011-06-21_17-17-35.txt).
@echo off SETLOCAL ENABLEDELAYEDEXPANSION FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC ^Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO ( IF %%A GTR 0 ( SET Day=%%A SET Hour=%%B SET Min=%%C SET Month=%%D SET Sec=%%E SET Year=%%F ) ) if %Month% LSS 10 set Month=0%Month% if %Day% LSS 10 set Day=0%Day% if %Min% LSS 10 set Minute=0%Minute% if %Hour% LSS 10 set Hour=0%Hour% set now=%year%-%month%-%day%_%hour%-%min%-%sec% set now=%now% echo hello world > log-%now%.txt