3

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

4 Answers4

3

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

0

Given a layout of e.g.:

Main Folder 
     Sub Folder A
        Sub Folder 1
        [...]
        Sub Folder 4
     Sub Folder B 
        Sub folder 1 
        [...]
        Sub Folder 4 
     [...]

The simplest answer (even if it is rather tedious and brute force) is to simply write out mkdir lines for each final subfolder:

mkdir "Main Folder\Sub Folder A\Sub Folder 1"
mkdir "Main Folder\Sub Folder A\Sub Folder 2"
mkdir "Main Folder\Sub Folder A\Sub Folder 3"
mkdir "Main Folder\Sub Folder A\Sub Folder 4"

Repeat this as many times as you need and simply change the folder names.

If a directory doesn't initially exist, it will be created. If it does exist, a folder will be created in it. So the very first line:

mkdir "Main Folder\Sub Folder A\Sub Folder 1"

will create all three folders shown whereas this line:

mkdir "Main Folder\Sub Folder Z\Sub Folder 1"

creates only the 2nd and 3rd folder (since Main Folder already exists).

As you indicated you wanted special characters (spaces and commas), you'll need to wrap each command with quotes (as shown). For folders without any special characters (e.g. Folder1\Folder2 ), quotes are unnecessary.

I haven't included drive letters since I am assuming the .bat file will be in the same directory as where you wish to create Main Folder and its subfolders.

Anaksunaman
  • 18,227
0

powershell might do it.

$root = "\\*\folderA"

ForEach ($dir in (Get-Item -Path "$root\*\" | ?{$_.PSIsContainer})){

If (!(Test-Path -Path "\*\FolderA")) {

    New-Item -Path "$root\*\" -Name "Folder1" -ItemType Directory | Out-Null

}

}

Just repeat the New-Item -Path "$root\*\" -Name "Folder1" -ItemType Directory | Out-Null part for each subfolder

0

Another way would be:

mkdir -p main_folder/{bio,gallery,home,howto,infofeeds,links,projects}/{static,templates}
Stonecraft
  • 360
  • 5
  • 15