windows - Create Directory using Batch Script with Timestamp -
@echo off /f "tokens=2-4 delims=/ " %%a in ('date /t') (set mydate=%%c-%%a-%%b) /f "tokens=1-2 delims=/:" %%a in ('time /t') (set mytime=%%a-%%b) set mydir="%mydate%-%mytime%" mkdir %mydir%
with above batch script, can create directory name 2015-05-14-11-30 am
now need convert time 24 format , remove am/pm
expected folder name - 2015-05-14-11-30
how ?
i think should use %time%
pseudo-variable.
this example:
@echo off /f "tokens=1-4 delims=/:." %%a in ("%time%") ( set hh24=%%a set mi=%%b set ss=%%c set ff=%%d ) /f "tokens=1-2 delims=/,." %%a in ("%ss%") ( set just_ss=%%a ) echo hh24=%hh24% echo mi=%mi% echo ss=%ss% echo ff=%ff% echo just_ss=%just_ss% echo mytime=%hh24%-%mi%-%just_ss%
well, realized hh24 has empty space if hour less 10, here have solution, using %time
:
set hh=%time:~0,2% if "%hh:~0,1%" == " " set hh=0%hh:~1,1% echo hh=%hh% set mi=%time:~3,2% if "%mi:~0,1%" == " " set mi=0%mi:~1,1% echo mi=%mi% set ss=%time:~6,2% if "%ss:~0,1%" == " " set ss=0%ss:~1,1% echo ss=%ss%
following advice pointed out @stephan (time
variable dependent on region settings), can use approach:
@echo off /f "delims=" %%a in ('wmic os localdatetime ^| find "."') set datetime=%%a set "yyyy=%datetime:~0,4%" set "mm=%datetime:~4,2%" set "dd=%datetime:~6,2%" set "hh=%datetime:~8,2%" set "mi=%datetime:~10,2%" set "ss=%datetime:~12,2%" set fullstamp=%yyyy%-%mm%-%dd%-%hh%-%mi%-%ss% echo fullstamp=%fullstamp%
Comments
Post a Comment