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)
With the current date but without the separator "-" (ex. 20081108.dat)
echo hello > %date%.dat
To use the current time we must remove the separator ":" because it's not permitted in a filename. (ex. 20081108220221). Since %time% return also a ",", you can't specified an extension.
echo hello > %date:-=%.dat
If you really want an extension (ex. 2008110822022107.dat), try this
echo hello > %date:-=%%time::,=%
Here a better formatted solution (ex. log-2011-06-20_22-46-40,62.txt)
set x=%date:-=%%time::,=%.dat set x=%x:,=% echo hello > %x%
Since Windows use the short format date as defined the regional settings, you can be in trouble if the current installation sets the short date format to something different than YYYY-MM-DD.
@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