• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 604
  • Last Modified:

Script to Create folders with sub folders

I would like a script to create folders with sub-folders with changing numbers with each parent folder. Example as followed

create folders on the x:\ drive

2010 - Parent
01-0001 - Sub of Parent    (this folder needs to change for each on that i create up to the number 01-3000)
Photos - sub of Sub
Diagrams - sub of Sub
Misc - sub of Sub
Statements - sub of Sub

if this does not make sense i am also attaching an image of what i am wanting to accomplish

Untitled.jpg
0
k_hoffhines
Asked:
k_hoffhines
2 Solutions
 
k_hoffhinesAuthor Commented:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim BaseFldr
BaseFldr = "X:\Directory Name"

Dim Sub1, Sub2, Sub3, Sub4, Sub5
Sub1 = "Folder 1"
Sub2 = "Folder 2"
Sub3 = "Folder 3"
Sub4 = "Folder 4"
Sub5 = "Folder 5"

Dim i, s

If NOT fso.FolderExists(BaseFldr) Then fso.CreateFolder(BaseFldr)

For i = 0 to 10000
s = BaseFldr & "\" & Right("0000" & i, 5)
If NOT fso.FolderExists(s) Then fso.CreateFolder(s)
If NOT fso.FolderExists(s & "\" & Sub1) Then fso.CreateFolder(s & "\" & Sub1)
If NOT fso.FolderExists(s & "\" & Sub2) Then fso.CreateFolder(s & "\" & Sub2)
If NOT fso.FolderExists(s & "\" & Sub3) Then fso.CreateFolder(s & "\" & Sub3)
If NOT fso.FolderExists(s & "\" & Sub4) Then fso.CreateFolder(s & "\" & Sub4)
If NOT fso.FolderExists(s & "\" & Sub5) Then fso.CreateFolder(s & "\" & Sub5)
Next

MsgBox "Finished Creating Folders"
0
 
Shift-3Commented:
Your example says the subfolders should start with 01-, but your screenshot shows 10-.  I'm going on the assumption that you meant the prefix should be taken from the last two characters of the parent folder's name.

Paste the script below into a text file with a .cmd extension.  Customize the value of the root variable on line 4 with the location of the folder containing the yearly folders.  Customize the value of the max variable on line 5 with the number of new sets of folders to create (it's set to 3 for testing, but can be increased to 3000).

Running the script will echo the commands to be run.  Once you have tested it successfully and are sure it will do what you intend, remove the word ECHO from lines 14-17 to create the folders.


@echo off
setlocal enabledelayedexpansion
 
set root=x:\files
set max=3
 
for /f "tokens=*" %%G in ('dir "%root%" /a:d /b') do (
 set prefix=%%G
 set prefix=!prefix:~-2!
 
 for /l %%H in (1, 1, %max%) do (
  set count=000%%H
  set count=!count:~-4!
  ECHO md "%root%\%%G\!prefix!-!count!\Diagram" 2> NUL
  ECHO md "%root%\%%G\!prefix!-!count!\Misc" 2> NUL
  ECHO md "%root%\%%G\!prefix!-!count!\Photos" 2> NUL
  ECHO md "%root%\%%G\!prefix!-!count!\Statements" 2> NUL
 )
)
 
pause

Open in new window

0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now