razumny
razumny

Reputation: 127

Batch create a set of subfolders in several folders

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!

Upvotes: 0

Views: 3191

Answers (2)

Aacini
Aacini

Reputation: 67216

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.

Upvotes: 6

PA.
PA.

Reputation: 29339

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!

Upvotes: 3

Related Questions