Tuesday, October 28, 2008

Batch .bat scripting, output current date todayb


@ECHO OFF
REM Default to tokens 1 to 3.
SET UnivDateTokens=1-3

REM Determine the first token.
FOR /F "tokens=1 delims=.:/-, " %%A IN ('DATE /T') DO SET UnivDatePart1=%%A

REM If the first token is a letter then use tokens 2 to 4 instead.
IF "%UnivDatePart1:~0,1%" GTR "9" SET UnivDateTokens=2-4

REM Extract the current date and put it into the yy, mm, dd variables depending upon the system date format.
FOR /F "tokens=%UnivDateTokens% delims=.:/-, " %%A in ('DATE /T') DO (
FOR /F "skip=1 tokens=2-4 delims=/-,()." %%D IN ('ECHO.^|DATE') DO (
SET %%D=%%A
SET %%E=%%B
SET %%F=%%C
SET UnivDatePart1=
SET UnivDateTokens=
)
)

REM Create a shortcut to the current date/time in CCYYMMDD format.
SET TodaysDateTime=%YY%-%MM%-%DD%

echo %TodaysDateTime%

@PAUSE

No comments: