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

5,082

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:
5,082

Related videos on Youtube

Steve Reeder
Author by

Steve Reeder

Updated on September 18, 2022

Comments

  • Steve Reeder
    Steve Reeder over 1 year

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

    • user1686
      user1686 over 11 years
      Which parts does the user input, and which parts have to be calculated?
    • Steve Reeder
      Steve Reeder over 11 years
      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.
  • Steve Reeder
    Steve Reeder over 11 years
    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.