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

前端 未结 24 1252
被撕碎了的回忆
被撕碎了的回忆 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:54
    mkdir %date:~-4,4%%date:~-10,2%%date:~7,2%
    
    0 讨论(0)
  • 2020-12-08 02:54
    G:
    
    cd G:/app/
    
    mkdir %date:~7,2%%date:~-10,2%%date:~-4,4% 
    
    cd %date:~7,2%%date:~-10,2%%date:~-4,4% 
    
    sqlplus sys/sys as sysdba @c:/new
    
    0 讨论(0)
  • If your locale has date format "DDMMYYYY" you'll have to set it this way:

    set datestr=%date:~-4,4%%date:~3,2%%date:~-10,2%
    mkdir %datestr%
    
    0 讨论(0)
  • 2020-12-08 02:57

    You need to get rid of the '/' characters in the date before you can use it in mkdir like this:

    setlocal enableextensions
    set name=%DATE:/=_%
    mkdir %name%
    
    0 讨论(0)
  • 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%"
    
    0 讨论(0)
  • 2020-12-08 02:59

    This works for me, try:

    ECHO %DATE:~7,2%_%DATE:~4,2%_%DATE:~12,2%
    
    0 讨论(0)
提交回复
热议问题