How do I write a batch script to generate folders for each month, day and year?

2

1

How do I write a batch script to generate folders for each month, day and year based on user input?

Richie086

Posted 2012-10-03T17:35:04.880

Reputation: 4 299

Which parts does the user input, and which parts have to be calculated? – user1686 – 2012-10-03T17:46:24.870

1the user input is the year, sorry for not making that clear. So, %INPUT% variable is set to a 4 digit year. For example, if you enter 2012, a folder named 2012 is created and then the month folders are created. Next the MM-DD folders are created inside of each NN-MMM folder. – Richie086 – 2012-10-03T17:50:02.087

Answers

12

Handles leap years:

@echo off & setlocal
set year=%1
if "%year%"=="" set /p year=Year? 
if "%year%"=="" goto :eof
set /a mod=year %% 400
if %mod%==0 set leap=1 && goto :mkyear
set /a mod=year %% 100
if %mod%==0 set leap=0 && goto :mkyear
set /a mod=year %% 4
if %mod%==0 set leap=1 && goto :mkyear
set leap=0

:mkyear
call :mkmonth 01 Jan 31
call :mkmonth 02 Feb 28+leap
call :mkmonth 03 Mar 31
call :mkmonth 04 Apr 30
call :mkmonth 05 May 31
call :mkmonth 06 Jun 30
call :mkmonth 07 Jul 31
call :mkmonth 08 Aug 31
call :mkmonth 09 Sep 30
call :mkmonth 10 Oct 31
call :mkmonth 11 Nov 30
call :mkmonth 12 Dec 31
goto :eof

:mkmonth
set month=%1
set mname=%2
set /a ndays=%3
for /l %%d in (1,1,9)        do mkdir %year%\%month%-%mname%\%month%-0%%d
for /l %%d in (10,1,%ndays%) do mkdir %year%\%month%-%mname%\%month%-%%d

user1686

Posted 2012-10-03T17:35:04.880

Reputation: 283 655

Wow, that is much better than my script! And in reference to the banner, I like letting people know what it is a script is doing rather than just asking What Year?... Thats the only reason why I included it. Thanks for posting your version of this script tho, it gets the same job done with less code. – Richie086 – 2012-10-03T20:05:25.670