Page 1 of 1

Batch File To Create Folder With Current Date

Posted: 16 Oct 2023 12:46
by data808
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.

Re: Batch File To Create Folder With Current Date

Posted: 16 Oct 2023 13:38
by miskox
See viewtopic.php?p=60703#p60703

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

Saso

Re: Batch File To Create Folder With Current Date

Posted: 16 Oct 2023 19:05
by data808
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.

Re: Batch File To Create Folder With Current Date

Posted: 16 Oct 2023 20:58
by Batcher
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

Re: Batch File To Create Folder With Current Date

Posted: 16 Oct 2023 20:59
by Batcher
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

Re: Batch File To Create Folder With Current Date

Posted: 22 Oct 2023 09:14
by DOSadnie
You might want also to take a look at this topic: viewtopic.php?f=3&t=10491

Re: Batch File To Create Folder With Current Date

Posted: 09 Nov 2023 14:15
by data808
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?

Re: Batch File To Create Folder With Current Date

Posted: 09 Nov 2023 15:21
by data808
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.

Re: Batch File To Create Folder With Current Date

Posted: 09 Nov 2023 15:29
by data808
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?

Re: Batch File To Create Folder With Current Date

Posted: 09 Nov 2023 15:32
by ShadowThief

Code: Select all

set "YourLocation=%~dp0"

Re: Batch File To Create Folder With Current Date

Posted: 09 Nov 2023 20:15
by Batcher
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

Re: Batch File To Create Folder With Current Date

Posted: 31 Jan 2024 12:40
by data808
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%"

Re: Batch File To Create Folder With Current Date

Posted: 31 Jan 2024 13:27
by Squashman
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.

Re: Batch File To Create Folder With Current Date

Posted: 31 Jan 2024 13:46
by Squashman
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

Re: Batch File To Create Folder With Current Date

Posted: 31 Jan 2024 17:33
by data808
Ok thank you for confirming that. It must be a connection lag issue with the network drive then.