Create folder with current date

April 24, 2011 at 18:42:10
Specs: Windows 7
Hi, can someone help me with this batch file, i was making it so that it will create a folder with current date but the date is set to a numeric value and i want to change the month so that it will show the actual month (ex. April, March). I am using windows 7 and just trying to polish the main script i am creating. thanks
---------------------------
@echo off
set folder1=%date:~6,4%
set folder2=%date:~3,2%
set folder3=%date:~0,2%
mkdir "c:\downloaded Cisco Calls\%folder1%\%folder2%\%folder3%"
----------------------------------

See More: Create folder with current date

Report •


#1
April 24, 2011 at 22:52:41
Unless the date issues have been fixed in Win7 the batch %date%
variable is settings dependant, thus the script won't work right
if the settings are changed.


***********************************************************
If you are still ok with using %date% then you could use
something like this:

set month_word=<the number of the month>

for %%a in ("10=October" "11=November" "12=December" "1=January" "2=February") do (
    call set month_word=%%month_word:%%~a%%
)
for %%a in ("3=March" "4=April" "5=May" "6=June" "7=July" "8=August" "9=September") do (
    call set month_word=%%month_word:%%~a%%
)
echo %month_word%

Just replace "<the number of the month>" with the months number
(I can't tell if it's %folder2% or %folder3%).


Report •

#2
April 26, 2011 at 07:28:06
@Judago - Yeah i think using %date% will still works for me. I only need to adjust a couple of numbers if i need to. Anyway I tested the batch script and it works really great. I'm just new to batch scripting and i just trying to learn as i make the script. if you don't mind could you explain this part thanks!

"call set month_word=%%month_word:%%~a%%"


--------------------------------------------------------------------------------------
@echo off

set folder1=%date:~6,4%
set folder2=%date:~3,2%
set folder3=%date:~0,2%
set month_word=%folder2%

for %%a in ("10=October" "11=November" "12=December" "01=January" "02=February") do (
call set month_word=%%month_word:%%~a%%
)
for %%a in ("03=March" "04=April" "05=May" "06=June" "07=July" "08=August" "09=September") do (
call set month_word=%%month_word:%%~a%%
)
mkdir "c:\downloaded Cisco Calls\%folder1%\%month_word%\%folder3%"
pause


Report •

#3
April 26, 2011 at 17:38:51
No problem.

The explaination:

The for loop passes strings like "10=October", with the double quotes, to the "do" part of the loop, the double quotes are important because for will split unquoted strings on "=;,<space><tab>".

%%~a takes the string like "10=October" and removes the double quotes. Batch Environment variables support string replacement (%var:str=rep% or !var:str=rep!), %%~a contains both the string and replacement. Since it's just a string replace you can see why 10,11 and 12 are done first.

Standard environment %variables% don't normally update in loops or if statements. The "call" and the doubled percentage signs (the for variable is unchanged) allow limited use of the variable, enough for the month change. Be aware the the call method won't work with all commands.

Instead of using "call" delayed expansion could be turned or an !'s used

SetLocal EnableDelayedExpansion
.....
.....    set month_word=!month_word:%%~a!


Report •

Related Solutions

#4
May 1, 2011 at 08:14:35
Thank you very much for the help, very much appreciated!

Report •

Ask Question