Script to Create folders with sub folders

Posted on 2009-12-30
Last Modified: 2012-05-08
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

Question by:k_hoffhines

    Accepted Solution

    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)

    MsgBox "Finished Creating Folders"
    LVL 38

    Assisted Solution

    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

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    This article is an update and follow-up of my previous article:   Storage 101: common concepts in the IT enterprise storage This time, I expand on more frequently used storage concepts.
    Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now