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

前端 未结 24 1216
被撕碎了的回忆
被撕碎了的回忆 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:32
    setlocal enableextensions 
    set name="%DATE:/=_%"
    mkdir %name%
    

    to create only one folder like "Tue 01_28_2020"

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

    the expression %date:~p,n% returns n number of characters from position p in the date string.

    if my system date string is Mon23/11/2015

    the command %date:~1,3% returns the value Mon

    the command %date:~10,4% returns the value 2015

    and in conjunction with the md (or mkdir) command

    the command md %date:~10,4%%date:~7,2%%date:~4,2% makes a directory named 20151123

    likewise if your date string in in the format Monday, 23/Nov/2015

    the command md %date:~16,4%%date:~12,3%%date:~9,2% makes a directory named 2015Nov23

    If you accidentally return characters from the date string that are not allowed in folder names or use invalid values for p and n you will get an error. Additionally if you return values that include \ this may create a folder within a folder.

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

    I use the next code to make a file copy (e.g. test.txt) before replacing:

    cd /d %~dp0
    set backupDir=%date:~7,2%-%date:~-10,2%-%date:~-2,2%_%time:~0,2%.%time:~3,2%.%time:~6,2%
    echo make dir %backupDir% ...
    md "%backupDir%"
    copy test.txt %backupDir%
    

    It creates directory in format DD-MM-YY_HH.MM.SS and places text.txt there. Time with seconds in the name is necessary to create directory without additional verification.

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

    I am sitting in exactly the same boat as you as soon as i am AM before 10 i cannot use the below, i have set my time from 12hr to 24 hr, changed hh/mm to HH/mm I have tried most of the codes i could find. below will help at least a little. tweak and fix :)

    Below may help also

    set DD=%DATE:~0,2%

    set MM=%DATE:~3,2%

    set YY=%DATE:~8,2%

    set YYYY=%DATE:~6,4%

    set hh=%hh: =0%

    set mm=%TIME:~3,2%

    if "%time:~0,1%" == " " (set folderdate=0%time:~1,1%) ELSE set folderdate=%time:~0,2%

    mkdir folderdate=%date:~6%%date:~3,2%%date:~0,2%_%folderdate%%time:~3,2%

    copy \Makereport*.CSV \Makereport\%folderdate%\

    cd %folderdate% REM -( 7zip in c:\batch) Path = c:\batch

    7z a Retail.zip *.CSV -pRetailPassword

    cd..

    del *.csv

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

    Use this batch script made by me:

    @echo off
    title Folder Creator
    color b
    setlocal enabledelayedexpansion
    echo Enter the folder name, you can use these codes:
    echo /t - Time (eg. 16:29)
    echo /d - Date (eg. 17-02-19)
    echo /a - Day (eg. 17)
    echo /m - Month (eg. 02)
    echo /y - Year (eg. 19)
    echo /f - Full Year (eg. 2019)
    echo.
    set /p foldername=Folder Name:
    set foldername=%foldername:/t=!time:~0,5!%
    set foldername=%foldername:/d=!date:~0,2!-!date:~3,2!-!date:~8,2!%
    set foldername=%foldername:/a=!date:~0,2!%
    set foldername=%foldername:/m=!date:~3,2!%
    set foldername=%foldername:/y=!date:~8,2!%
    set foldername=%foldername:/f=!date:~6,4!%
    md %foldername%
    

    For example if you wanted to make a folder named the date in the DD-MM-YY format you would type "/d" but if you wanted to do that in the DD-MM-YYYY format you would type "/a-/m-/f".

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

    I needed both the date and time and used:

    mkdir %date%-%time:~0,2%.%time:~3,2%.%time:~6,2%
    

    Which created a folder that looked like: 2018-10-23-17.18.34

    The time had to be concatenated because it contained : which is not allowed on Windows.

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