Batch Script to Create Multiple Folders within Multiple Folders

batch file

I have tried a bunch of things and a lot of research but nothing I've tried has done what I need.

I'm trying try create a folder structure of multiple folders within another folder. The names of the folders are separated by commas in a batch script loop.

I now need to create another heap of subfolders but I need them to be beneath the first level subfolder that was created by the other list separated by commas folder names.

Example Folder Structure

 - Main folder
   - Sub folder A
      - Sub folder 1
      - Sub folder 2
      - Sub folder 3
      - Sub folder 4 
   - Sub folder B
      - Sub folder 1
      - Sub folder 2
      - Sub folder 3
      - Sub folder 4 
   - Sub folder C
      - Sub folder 1
      - Sub folder 2
      - Sub folder 3
      - Sub folder 4

I've managed to create all the "main" sub-folders A,B,C but I have had no luck in creating the sub-folders 1,2,3.. from the new heap beneath the first heap of sub-folders create as A,B,C

for %%x in (A,B,C) do md %%x

Best Answer

You can create a variable with the A,B,C set, create another variable with the 1,2,3,4 set, and then put each set in a nested FOR loop and iterate each variable value accordingly from each set to get A1, A2, A3, B1, B2, B3 and so forth ensuring every combination is iterated.

You just use the MD command to create the directory combinations appended to the root directory where the main subfolders from group A set separated by commas will reside.

Script Example

Be sure to set the RootDir= value as the full path to the parent folder that will contain the group A list folders that are set as the SubA= variable.

@ECHO ON
SET RootDir=C:\Main
SET SubA=A,B,C,D,E
SET SubB=1,2,3,4,5
FOR %%A IN (%SubA%) DO FOR %%B IN (%SubB%) DO IF NOT EXIST "%RootDir%\%%~A\%%~B" MD "%RootDir%\%%~A\%%~B"
EXIT

Further Resources

Related Question