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

前端 未结 24 1217
被撕碎了的回忆
被撕碎了的回忆 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:37

    This should work:

    mkdir %date%
    

    If it doesn't, try this:

    setlocal enableextensions
    mkdir %date%
    
    0 讨论(0)
  • 2020-12-08 02:38

    This depends on the regional settings of the computer, so first check the output of the date using the command prompt or by doing an echo of date.

    To do so, create a batch file and add the below content

    echo %date%    
    pause
    

    It produces an output, in my case it shows Fri 05/06/2015.

    Now we need to get rid of the slash (/)

    For that include the below code in the batch file.

    set temp=%DATE:/=%
    

    if you echo the "temp", you can see the date without the slash in it.


    Now all you need to do is formatting the date in the way you want.

    For example I need the date in the format of YYYYMMDD, then I need to set the dirname as below

    To explain how this works, we need to compare the value of temp

    Fri 05062015.

    now position each characters with numbers starting with 0.

    Fri 0506201 5

    01234567891011

    So for the date format which I need is 20150605,

    The Year 2015, in which 2 is in the 8th position, so from 8th position till 4 places, it will make 2015.

    The month 06, in which 0 is in the 6th position, so from 6th position till 2 places, it will make 06.

    The day 05, in which 0 is in the 4th position, so from 4th position till 2 places, it will make 05.

    So finally to set up the final format, we have the below.

    SET dirname="%temp:~8,4%%temp:~6,2%%temp:~4,2%"
    

    To enhance this date format with "-" or "_" in between the date, month and year , you can modify with below

    SET dirname="%temp:~8,4%-%temp:~6,2%-%temp:~4,2%"
    

    or

    SET dirname="%temp:~8,4%_%temp:~6,2%_%temp:~4,2%"
    

    So the final batch code will be

    ======================================================

    @echo off    
    set temp=%DATE:/=%
    set dirname="%temp:~8,4%%temp:~6,2%%temp:~4,2%"
    mkdir %dirname%
    

    ======================================================

    The directory will be created at the place where this batch executes.

    0 讨论(0)
  • 2020-12-08 02:39

    If you want mm-dd-yyyy format you can use:

    mkdir %date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%
    
    0 讨论(0)
  • 2020-12-08 02:39

    this worked better for me,

    @echo off    
    set temp=%DATE:/=%
    set dirname="%temp:~4,4%%temp:~2,2%%temp:~0,2%"
    mkdir %dirname%
    
    0 讨论(0)
  • 2020-12-08 02:43

    Try this (an equivalent of bash backquotes):

    for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
    mkdir %datestr%
    

    For further information, see http://ss64.com/nt/for_cmd.html

    0 讨论(0)
  • 2020-12-08 02:45
    for /F “tokens=1-4 delims=/ ” %%A in (‘date /t’) do (
        set DateDay=%%A
        set DateMonth=%%B
        set DateYear=%%C
    )
    set CurrentDate=%DateDay%-%DateMonth%-%DateYear%
    md %CurrentDate%
    

    This will give you a newly created folder with today’s date, in the format of DD-MM-YY

    Sourced from: Ali's Knowledge Base

    0 讨论(0)
提交回复
热议问题