This is a locale-independent solution (copy to a file named SetDateTimeComponents.cmd):
@echo off REM This script taken from the following URL: REM http://www.winnetmag.com/windowsscripting/article/articleid/9177/windowsscripting_9177.html REM Create the date and time elements. for /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $d $t"') do ( for /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do ( set dow=%%i set %%a=%%j set %%b=%%k set %%c=%%l set hh=%%m set min=%%n set ss=%%o ) ) REM Let see the result. echo %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss%
I put all my .cmd scripts in the same folder (% SCRIPTROOT%); any script that needs date / time values ββwill call SetDateTimeComponents.cmd, as in the following example:
setlocal @echo Initializing... set SCRIPTROOT=%~dp0 set ERRLOG=C:\Oopsies.err :: Log start time call "%SCRIPTROOT%\SetDateTimeComponents.cmd" >nul @echo === %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss% : Start === >> %ERRLOG% :: Perform some long running action and log errors to ERRLOG. :: Log end time call "%SCRIPTROOT%\SetDateTimeComponents.cmd" >nul @echo === %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss% : End === >> %ERRLOG%
As the example shows, you can call SetDateTimeComponents.cmd when you need to update the date / time values. Hiding the parsing script in itβs own SetDateTompocents.cmd file is a good way to hide ugly details and, more importantly, avoid typos.
totorocat Jun 30 '09 at 18:15 2009-06-30 18:15
source share