0

My goal is to create a set of folders with identical subfolders. I need one folder for every year since 1881, each year-folder should contain a folder per month, and each month-folder should contain a folder per day.

I have found a tool to create the folders for the years, my challenge now is to populate them with the subfolders. I want to do this on a Windows client computer, preferably without installing anything.

What I have been working on, is using the FOR and MD commands to do the job. Here's the code I have so far:

SET %g=*.* FOR /d %var IN %path% DO MKDIR 01 02 03 

Whenever I run this, I get this error:

%path% was unexpected at this time

So, that is where I am stuch right now. I'd appreciate any help you can give me!

2 Answers 2

6

Excuse me P.A. Your solution is right, I just couldn't resist the temptation to add some code to create the right number of days per month.

@echo off setlocal EnableDelayedExpansion set m=0 for %%d in (31 28 31 30 31 30 31 31 30 31 30 31) do ( set /A m+=1 set daysInMonth[!m!]=%%d ) pushd d:\dest for /L %%y in (1881,1,2012) do ( mkdir %%y pushd %%y for /L %%m in (1,1,12) do ( mkdir %%m pushd %%m set days=!daysInMonth[%%m]! if %%m == 2 ( set /A yMod4=%%y %% 4, yMod100=%%y %% 100, yMod400=%%y %% 400 if !yMod4! == 0 ( set /A days+=1 if !yMod100! == 0 if not !yMod400! == 0 ( set /A days-=1 ) ) ) for /L %%d in (1,1,!days!) do ( mkdir %%d ) popd ) popd ) popd 

Previous code add 1 day to February in leap years, that is, if the year is divisible by 4, but at centurial years only if it is also divisible by 400. 1600 and 2000 were leap years, but 1700, 1800 and 1900 were not.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for an always correct answer, but do you excpect that one of your leap year exceptions will ever be used?
@jeb: Well, the OP requires to create year folders for 1900 and 2000. Also, think about this: Batch files were invented more than 30 years ago and we are here using them! So, who knows for the 2100? :)
+1 good solution: some optimization/simplification - Computation of days in month can be reduced to set /a "days=daysInMonth[%%m]+(^!(%%m-2)&^!(%%y%%4)&(^!^!(%%y%%100)|^!(%%y%%400)))". No need for pushd and intermediate mkdir. Simply use this in inner most loop: for /L %%d in (1,1,!days!) do mkdir d:\dest\%%y\%%m\%%d.
3

first, read HELP FOR and then to begin with something, try this in a command line

 for /l %a in (1881,1,2012) do @echo %a 

now you're wet already, add some spice

 for /l %a in (1881,1,2012) do @for /l %b in (1,1,12) do @echo %a-%b 

and you're almost done

 for /l %a in (1881,1,2012) do @for /l %b in (1,1,12) do @for /l %c in (1,1,31) do @echo %a-%b-%c 

the only thing left is to transform your echo into the appropiate mkdir and adding some incantation to translate it into a BAT file....

@echo off pushd d:\dest for /l %%a in (1881,1,2012) do ( mkdir %%a pushd %%a for /l %%b in (1,1,12) do ( mkdir %%b pushd %%b for /l %%c in (1,1,31) do ( mkdir %%c ) popd ) popd ) popd 

but, be warned, this will grow extremely crazy!

1 Comment

No need for pushd/popd and mkdir only needed in inner-most loop: mkdir d:\dest\%%a\%%b\%%c

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.