Batch File To Create Folder With Current Date
Moderator: DosItHelp
Batch File To Create Folder With Current Date
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.
Thank you for your help on this.
Re: Batch File To Create Folder With Current Date
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.
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
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
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
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
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?Batcher wrote: ↑16 Oct 2023 20:59test-2.batCode: 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
Ok never mind. I got it to work but I was wondering, how do I change the date format to YYYY-MM-DD instead?Batcher wrote: ↑16 Oct 2023 20:59test-2.batCode: 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
I think that would work out better for sorting purposes.
Re: Batch File To Create Folder With Current Date
Thanks. I figured it out for the date format.Batcher wrote: ↑16 Oct 2023 20:59test-2.batCode: 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
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?
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Batch File To Create Folder With Current Date
Code: Select all
set "YourLocation=%~dp0"
Re: Batch File To Create Folder With Current Date
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
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:
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.
Reason: Please use CODE tags.
Re: Batch File To Create Folder With Current Date
Incorrect. Whatever user account runs the batch file would be the user account that is creating the folder.data808 wrote: ↑31 Jan 2024 12:40I 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.
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
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.data808 wrote: ↑09 Nov 2023 15:21Ok never mind. I got it to work but I was wondering, how do I change the date format to YYYY-MM-DD instead?Batcher wrote: ↑16 Oct 2023 20:59test-2.batCode: 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
I think that would work out better for sorting purposes.
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
Ok thank you for confirming that. It must be a connection lag issue with the network drive then.