Thursday, April 25, 2013

Put current date and time to string variable in .BAT files

As an example, following code puts current date and time into log file name.

This code does not depend on locale or regional settings.

@echo off

set x=
for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do if not defined x set x=%%x

set filename=%X:~0,4%-%X:~4,2%-%X:~6,2%_%X:~8,2%-%X:~10,2%-%X:~12,2%-%X:~15,3%.log
echo %filename%
Output is:

No comments:

Post a Comment