aza01
aza01

Reputation: 121

Batch Script to Create Multiple Folders within Multiple Folders

I'm trying try create a folder structure of multiple folders within another folder.

The below script creates the same subfolders but not what I need.

@ECHO ON
SET RootDir=C:\Main
SET SubA=A,B
SET SubB=Rates,Docs,Fee,Category,Grades,6000,7,8,Falcon
FOR %%A IN (%SubA%) DO FOR %%B IN (%SubB%) DO IF NOT EXIST "%RootDir%\%%~A\%%~B" MD "%RootDir%\%%~A\%%~B"
EXIT

I need suppport to modify the script to create as per required folder structure.

 - Main folder
   - A
      - Rates
      - Docs
      - Fee
      - Category
   - B
      - Grades
      - 6000
      - 7
      - 8
      - Falcon

Upvotes: -1

Views: 1631

Answers (3)

aza01
aza01

Reputation: 121

Below code is working for my question. Posted after testing.

@ECHO ON
@for %%I in ("Rates" "Docs" "Fee" "Category") do @md "C:\Main\A\%%~I" 2>nul
@for %%I in ("Grades" "6000" "7" "8" "Falcon") do @md "C:\Main\B\%%~I" 2>nul

Upvotes: 0

Magoo
Magoo

Reputation: 80138

@ECHO Off
SETLOCAL
rem The following settings for the directories are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

SET "dirs=A B C"
SET "sdirs=1 2 3 4 5 6 7 8 "extra 1" Stack"

SET /a #d=0,#sd=0
FOR %%e IN (%dirs%) DO SET /a #d+=1&MD "%sourcedir%\%%e" >NUL 2>nul
FOR %%e IN (%sdirs%) DO SET /a #sd+=1
SET /a #sdind=(#sd + #d - 1) / #d

:nextd
FOR %%e IN (%dirs%) DO SET "#dirname=%%~e"&CALL SET "dirs=%%dirs:*%%e=%%"&GOTO gotd
:gotd

SET /a scount=0

:nexts
FOR %%e IN (%sdirs%) DO CALL SET "sdirs=%%sdirs:*%%e=%%"&MD "%sourcedir%\%#dirname%\%%e" >NUL 2>nul&GOTO gots
:gots

SET /a scount+=1
IF NOT DEFINED sdirs GOTO done
IF %scount% geq %#sdind% GOTO nextd
GOTO nexts

:done

DIR /s/b /ad "%sourcedir%"

GOTO :EOF

This is aimed at being a general solution. The "subdirectories" could be a list of filenames, for instance and the md subdirectory becomes move file destination.

Dealing with poison characters, existing directories or files etc. is not considered. There may be no trailing spaces in sdirs.

The objective is to make the subdirectories/move the files so that there is an even distribution as far as possible.

Count the directories and files to #dirs, #sdirs, then calculate the number of elements to move in #sdind.

Then grab the first element of the list and remove it from the list in each case, creating/moving as appropriate until the count in #sdind is reached, then step on to the next until done.

Upvotes: 0

user7818749
user7818749

Reputation:

The assumption here is that you want to create the subfolders dynamically, even if you have more sub folders in C:\Main

My other assumption is that the subfolders being numerical here is probably just a use case you gave to simplify the question.

I am posting this answer based on the examples you requested, if your real-world folders will be manually added as an environment variable, then this will not work with the numerical counter.

That is possible by iterating over the subfolders, then running a loop for each sub directory and creating the folders.

@echo off
setlocal enabledelayedexpansion
set "rootdir=C:\Main"
set "subs=A,B"
set "cnt=0"
for %%i in (%subs%) do (
    for /l %%a in (1,1,4) do (
        set /a cnt+=1
        md "%rootdir%\%%i\!cnt!" 2>nul
  )
)

This will work even if you have more sub folders, i.e C,D,E because of the for /l loop that increments only 4 counts per folder and will keep doing so until it has looped the entire subs environment variable.

Upvotes: 0

Related Questions