Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
Which parts does the user input, and which parts have to be calculated? –  grawity Oct 3 '12 at 17:46
1  
the 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 Oct 3 '12 at 17:50

1 Answer 1

up vote 10 down vote accepted

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
share|improve this answer
2  
Side note: I never quite understood why Windows batch script writers always add a large fancy banner with the script's name, the author's address (and often a misspelled one), and even a pause just to make sure the banner is seen. Scripts are supposed to make things convenient. Having to hit a key every time to skip the banner isn't convenient. –  grawity Oct 3 '12 at 18:25
    
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 Oct 3 '12 at 20:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.