Create SubFolders after Root Folder is created from xls file

Posted on 2008-10-22
Last Modified: 2012-05-05
Hello, I am trying to create subfolders underneath a root folder. I have say 10000 folders being created fine using the attached code.I am trying to figure out how to have subfolders created under each folder I have created using the excel spreadsheet data.

This is the structure i am looking to acheive.

                              SubFolder ( not part of xls data) just a folder that I will name
                              perhaps more subfolders here as well

So my code so far takes the data from xls and creates the 10000 folders i want.Underneath each of these 10000 folders I want to create other folders for each.

The bottom code snippet is what I am thinking but cant seem to get it right... can anyone help at all..

Thanks Very much
strComputer = "." 

strRoot = "z:\clients\10000\"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 

Set objExcel = CreateObject("Excel.Application")

Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\Clients10.xls") 

objExcel.Visible = False 

i = 1 

Do Until objExcel.Cells(i, 1).Value = ""     

    strNewFolderName = objExcel.Cells(i, 1).Value


 Set objFSO = CreateObject("Scripting.FileSystemObject")

 Set objFolder = objFSO.CreateFolder(strRoot & strNewFolderName) 




    i = i + 1




Set objFolder=objFSO.GetFolder(strRoot & strFolder)

Set colFolders=objFolder.SubFolders



For Each item In arrFolders

    If objFSO.FolderExists(strRoot & strFolder & "\" & item) Then

        WScript.Echo "Folder " & strRoot & strFolder &_

         "\" & item & " already exists"


        colFolders.Add item

    End If

Open in new window

Question by:Ottawaya
LVL 38

Accepted Solution

Shift-3 earned 250 total points
ID: 22780888
Assuming that I am understanding your intention correctly, this should do it.

Set objFolder=objFSO.GetFolder(strRoot)

Set colFolders=objFolder.SubFolders



For Each objSub in colFolders

    For Each item In arrFolders

        If objFSO.FolderExists(objSub.Path & "\" & item) Then

            WScript.Echo objSub.Path & "\" & item & " already exists"


            objFSO.CreateFolder objSub.Path & "\" & item

        End If



Open in new window

LVL 65

Expert Comment

ID: 22781216
Hi, perhaps a function like this would help?


Set objFSO = CreateObject("Scripting.FileSystemObject")

strPath = "C:\Temp\Folder1\Folder2"

If objFSO.FolderExists(strPath) = False Then CreateLocalDirectoryStructure strPath

Sub CreateLocalDirectoryStructure(strPath)

	If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)

	arrBits = Split(strPath, "\")

	strSubPath = arrBits(0)

	If UBound(arrBits) > 0 Then

		For intBit = 1 To UBound(arrBits)

			strSubPath = strSubPath & "\" & arrBits(intBit)

			If objFSO.FolderExists(strSubPath) = False Then objFSO.CreateFolder(strSubPath)


	End If

End Sub

Open in new window


Author Closing Comment

ID: 31508932
This is exactly what i was looking for I just appened it to ,y current script and volia.. all folders are there. I was on the right track.nest for loops i guess is the answer. I will ping back when i need more.. I have to now migrate data to these folders.. grr thats going to be a bigger task...

Thanks Much Shift-3

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

23 Experts available now in Live!

Get 1:1 Help Now