How to create a folder with name as current date in batch (.bat) files

前端 未结 24 1226
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 02:26

I don\'t know much about windows .bat file syntax. My simple requirement is to create a folder at a specific location with name as current date. I tried searching this on go

24条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 02:57

    https://stackoverflow.com/a/31789045/1010918 foxidrive's answer helped me get the folder with the date and time I wanted. I would like to share this method here since it worked great for me and I think it could help other people too, regardless of their locale.

    rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.
    
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"
    
     set "dirname=%YYYY%-%MM%-%DD% %HH%-%Min%-%Sec%"
    
     :: remove echo here if you like
     echo "dirName"="%dirName%"
    

提交回复
热议问题