bat – batch file to create formatted date time (ddmmyyyy) directory
I required to make a batch script which needs file / directory to be created with current date / time stamps.
Following are the code snipts for formating date / time in a batch script:
Following are the code snipts for formating date / time in a batch script:
FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘DATE/T’) DO SET MYDATE=%%B
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %MYDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/” %%A IN (‘echo %MYDATE%’) DO SET yyyy=%%B
set DATED=%mm%%dd%%yyyy%
md %DATED: =%
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %MYDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/” %%A IN (‘echo %MYDATE%’) DO SET yyyy=%%B
set DATED=%mm%%dd%%yyyy%
md %DATED: =%
or
FOR /F “tokens=*” %%A IN (‘DATE/T’) DO SET MYDATE=%%A
#Using regular expresions to remove / and DAY
SET MYDATE=%MYDATE:/=%
SET MYDATE=%MYDATE:* =%
md %MYDATE%
#Using regular expresions to remove / and DAY
SET MYDATE=%MYDATE:/=%
SET MYDATE=%MYDATE:* =%
md %MYDATE%
Other ways to achieve date string in a batch script variable:
@echo off
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set MYDATE=%yy%%mm%%dd%
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set MYDATE=%yy%%mm%%dd%
or
for /f “tokens=2-4 delims=/ ” %%g in (‘date /t’) do (
set mm=%%h
set dd=%%g
set yy=%%i
)
set MYDATE=%yy%%mm%%dd%
set mm=%%h
set dd=%%g
set yy=%%i
)
set MYDATE=%yy%%mm%%dd%
Similarly we can work with time:
for /f “tokens=1-2 delims=: ” %%j in (‘time /t’) do (
set hh=%%j
set mn=%%k
)
set MYTIME=%hh%%mn%
set hh=%%j
set mn=%%k
)
set MYTIME=%hh%%mn%
Further you can go ahead mixing time with date as well.
0 comments:
Post a Comment