To create a folder named after the current date in Windows cmd.exe

Asked

Viewed 10,576 times

5

What I already have: %date% is the windows variable that stores the current date. When I use echo %date% the value Tue 07/25/2017 is correctly printed.

But when I try to do mkdir %date% the created folder is Tue 07 because windows does not recognize / as a valid character.

How do I get around it?

  • 1

    The date format should be Tue 07/25/2017 same, or should only contain day, month and year?

  • 1

    It can be anything that can identify the date type 07-25-2017, 25072017, ...

4 answers

7

You can’t add / in the name of the folder. You should use a mask to date... Is usually used YYYYMMDD that is to say ANO MES e DIA, to make it easy to sort files by date...

Here’s an example of how to implement...

set filedatetime=%date% // ou qualquer outra data
set filedatetime=%filedatetime:~6,4%%filedatetime:~3,2%%filedatetime:~0,2%
echo "%filedatetime%" //exemplo da saida... "20170725"

then just give

mkdir %filedatetime%

Reference

  • 1

    that :~6,4 works like a function substring?

  • 3

    this... for example, the %filedatetime:~6,4% is picking up from the 6 character, the next 4, summarizing the 6 is the index and the 4 is the size...

4


Update Option to run a line:

The command wmic OS Get localdatetime /value returns the string LocalDateTime=20190609123708.733000-180, using 2 delimiters, =., we get 20190609123708, then we use output substring to compose the layout to create the folder:

%_data:~0,4%_%_data:~4,2%_%_data:~6,2%
   Ano:2019 _   Mês:06   _   Dia:09

At bat:

@echo off & for /f "tokens=2delims==." %%i in ('wmic OS Get localdatetime /value ^|findstr /r [0-9]')do set "_data=%%i" & call mkdir %_data:~0,4%_%_data:~4,2%_%_data:~6,2%

On the command line:

for /f "tokens=2delims==." %i in ('wmic OS Get localdatetime /value ^|findstr /r [0-9]')do set "_data=%i" & call mkdir %_data:~0,4%_%_data:~4,2%_%_data:~6,2%

Date format varies according to: region, language and/or settings made by the user

Reference:

Safe way to get Current day Month and year in batch

Parsing Dates in Batch Files & Regional Settings / Locale


To variable %date% will always display the date in the second format language & country, or in a format customized/configured by the user, that is to say, nay follows fixed layout/pattern, where we can work from predictive form, and, consequently, manipulate the output string, relying on a fixed/predictable layout

I suggest using the wmic, where, assuredly, you have a standardized exit and independent of any region, language and/or any settings made by user.

The command:

wmic Path Win32_LocalTime Get Day,Month,Year

Always returns in looping for:

[dia = 29 = %%a] [mês = 4 = %%b] [ano = 2019 = %%c]
 

After executing the command:

wmic Path Win32_LocalTime Get Day,Month,Year

We have the result:

Day  Month  Year
29   4      2019 
%%a  %%b    %%c

Script Dir_date.cmd:

@echo off & setlocal enabledelayedexpansion & set "_do=wmic Path Win32_LocalTime Get Day^,Month^,Year" 
for /f "tokens=1-3delims= " %%a in ('!_do!^|findstr /r [0-9]')do set "y=%%c" & set "d=0%%a" & set "m=0%%b"
2>nul mkdir !y!!m:~-2!!d:~-2! >nul 

Obs.: In Powershell, the layout can be customized with the ToString("yyyyMMdd"), where the options allow composing in the desired layout just by editing the strings with the ToString.

Example: yyyy-MM-dd, dd-MM-yyyy, MM-dd-yyyy, MM_dd_yyyy, yyyy_MM_dd, etc..


New-item -Name $(Get-Date).ToString("yyyyMMdd") -ItemType directory

2

Since the requirement is only to show the whole date, try:

mkdir %date:/=%

2

Swap the bars for hyphens by adding :/=- after date

mkdir %date:/=-%

Browser other questions tagged

You are not signed in. Login or sign up in order to post.