Batch File To Create Folder With Current Date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
data808
Posts: 47
Joined: 19 Jul 2012 01:49

Batch File To Create Folder With Current Date

#1 Post by data808 » 16 Oct 2023 12:46

How do I make a batch file that creates a folder in a specific location on a network drive and names the folder the current date (mm-dd-yyyy)?

Thank you for your help on this.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Batch File To Create Folder With Current Date

#2 Post by miskox » 16 Oct 2023 13:38

See viewtopic.php?p=60703#p60703

And I suggest you use yyyy-mm-dd and not mm-dd-yyyy.

Saso

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File To Create Folder With Current Date

#3 Post by data808 » 16 Oct 2023 19:05

Thanks for the reply and suggestion. I have a directory established already and the folders are all in MM-DD-YYYY format so I kind of want to keep it that way. I do see the benefit of making it YYYY-MM-DD if I had multiple folders with different years mixed together but I am going to keep it all separated by year so I don't think it will matter much.

I checked out the thread you suggested and it seems a bit advanced for me to understand. I'll keep looking at it to see if I can make sense of it but I really would like something simple like:

line of code (enter date format here)
line of code (enter directory here)

Is there anything anyone made that is simple like that to use?

Thanks.

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Batch File To Create Folder With Current Date

#4 Post by Batcher » 16 Oct 2023 20:58

test-1.bat

Code: Select all

@echo off
set "YourLocation=C:\Test"
for /f "tokens=2 delims==" %%i in ('wmic Path Win32_OperatingSystem get LocalDateTime /value ^| findstr "="') do (
    set "Today=%%i"
)
set "Today=%Today:~4,2%-%Today:~6,2%-%Today:~0,4%"
echo,%Today%
md "%YourLocation%\%Today%"
pause

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Batch File To Create Folder With Current Date

#5 Post by Batcher » 16 Oct 2023 20:59

test-2.bat

Code: Select all

@echo off
set "YourLocation=C:\Test"
for /f %%i in ('powershell "Get-Date -uformat '%%Y%%m%%d'"') do (
    set "Today=%%i"
)
set "Today=%Today:~4,2%-%Today:~6,2%-%Today:~0,4%"
echo,%Today%
md "%YourLocation%\%Today%"
pause

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Batch File To Create Folder With Current Date

#6 Post by DOSadnie » 22 Oct 2023 09:14

You might want also to take a look at this topic: viewtopic.php?f=3&t=10491

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File To Create Folder With Current Date

#7 Post by data808 » 09 Nov 2023 14:15

Batcher wrote:
16 Oct 2023 20:59
test-2.bat

Code: Select all

@echo off
set "YourLocation=C:\Test"
for /f %%i in ('powershell "Get-Date -uformat '%%Y%%m%%d'"') do (
    set "Today=%%i"
)
set "Today=%Today:~4,2%-%Today:~6,2%-%Today:~0,4%"
echo,%Today%
md "%YourLocation%\%Today%"
pause
When I try these out, the "YourLocation=C:\Test" which is what I paste the location of where I want the folder to go says that it's "not defined" in the terminal window. Then it will also mention that "A subdirectory or file \YourLocationToday already exists. Fyi, I also replaced this line "YourLocation" with the % signs of where I want the folder to go. The location I am using for both "YourLocation" is identical. Am I supposed to use something different?

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File To Create Folder With Current Date

#8 Post by data808 » 09 Nov 2023 15:21

Batcher wrote:
16 Oct 2023 20:59
test-2.bat

Code: Select all

@echo off
set "YourLocation=C:\Test"
for /f %%i in ('powershell "Get-Date -uformat '%%Y%%m%%d'"') do (
    set "Today=%%i"
)
set "Today=%Today:~4,2%-%Today:~6,2%-%Today:~0,4%"
echo,%Today%
md "%YourLocation%\%Today%"
pause
Ok never mind. I got it to work but I was wondering, how do I change the date format to YYYY-MM-DD instead?

I think that would work out better for sorting purposes.

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File To Create Folder With Current Date

#9 Post by data808 » 09 Nov 2023 15:29

Batcher wrote:
16 Oct 2023 20:59
test-2.bat

Code: Select all

@echo off
set "YourLocation=C:\Test"
for /f %%i in ('powershell "Get-Date -uformat '%%Y%%m%%d'"') do (
    set "Today=%%i"
)
set "Today=%Today:~4,2%-%Today:~6,2%-%Today:~0,4%"
echo,%Today%
md "%YourLocation%\%Today%"
pause
Thanks. I figured it out for the date format.

Is there a way to change this code's location where it saves the folder to always be where the batch file exists? So right now I have to set a location of my preference which works ok but I would also like to make a version of this to save the folder where ever the batch file is when I double click it to run it. Is that possible?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch File To Create Folder With Current Date

#10 Post by ShadowThief » 09 Nov 2023 15:32

Code: Select all

set "YourLocation=%~dp0"

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Batch File To Create Folder With Current Date

#11 Post by Batcher » 09 Nov 2023 20:15

test-2-v2.bat

Code: Select all

@echo off
cd /d "%~dp0"
for /f %%i in ('powershell "Get-Date -uformat '%%Y%%m%%d'"') do (
    set "Today=%%i"
)
set "Today=%Today:~4,2%-%Today:~6,2%-%Today:~0,4%"
echo,%Today%
md "%Today%"
pause

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File To Create Folder With Current Date

#12 Post by data808 » 31 Jan 2024 12:40

Hi Batcher. I been using the code you wrote for a while now and it works really well so thank you for that. However, sometimes I am noticing that I am having trouble deleting files in the folders that your code auto creates on a network drive. I am just trying to figure out if the network drive security settings or connection is the culprit or your code.

I get this feeling that it could be that our network drives are setup in a specific way so that the user that creates the folders may have certain permissions set to so they can edit or delete files within them and because, technically, your code is creating the folder instead of a user, that may be conflicting with the permissions of the folder.

What do you think about this theory? Is it possible to add on code that gives all users delete rights to the folder that your code creates? Here is the code I am running right now:

Code: Select all

@echo off
for /f %%i in ('powershell "Get-Date -uformat '%%Y%%m%%d'"') do (
    set "Today=%%i"
)

set "Today=%Today:~0,4%-%Today:~4,2%-%Today:~6,2%"
echo,%Today%

md "G:\Common\%Today%"
md "G:\Cabinet\%Today%"
Last edited by Squashman on 31 Jan 2024 13:48, edited 1 time in total.
Reason: Please use CODE tags.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File To Create Folder With Current Date

#13 Post by Squashman » 31 Jan 2024 13:27

data808 wrote:
31 Jan 2024 12:40
I get this feeling that it could be that our network drives are setup in a specific way so that the user that creates the folders may have certain permissions set to so they can edit or delete files within them and because, technically, your code is creating the folder instead of a user, that may be conflicting with the permissions of the folder.
Incorrect. Whatever user account runs the batch file would be the user account that is creating the folder.
If permissions are set correctly on the parent folder or network share there shouldn't be any issues.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File To Create Folder With Current Date

#14 Post by Squashman » 31 Jan 2024 13:46

data808 wrote:
09 Nov 2023 15:21
Batcher wrote:
16 Oct 2023 20:59
test-2.bat

Code: Select all

@echo off
set "YourLocation=C:\Test"
for /f %%i in ('powershell "Get-Date -uformat '%%Y%%m%%d'"') do (
    set "Today=%%i"
)
set "Today=%Today:~4,2%-%Today:~6,2%-%Today:~0,4%"
echo,%Today%
md "%YourLocation%\%Today%"
pause
Ok never mind. I got it to work but I was wondering, how do I change the date format to YYYY-MM-DD instead?

I think that would work out better for sorting purposes.
There was absolutely no need for the extra set command to change the date format. That can all be set in the Powershell command. Change to however you want it.

Code: Select all

PS C:\Users\Squashman> Get-Date -uformat '%Y-%m-%d'
2024-01-31
PS C:\Users\Squashman> Get-Date -uformat '%m-%d-%Y'
01-31-2024

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File To Create Folder With Current Date

#15 Post by data808 » 31 Jan 2024 17:33

Ok thank you for confirming that. It must be a connection lag issue with the network drive then.

Post Reply