Solved

How to recursively add missing directories

Posted on 2010-11-18
6
312 Views
Last Modified: 2012-05-10
How do I recursively add any required (missing) subdirectories using FSO?

For example, I want to have a function that will accept a folder name such as C:\folder1\folder2\folder3

What I need is to create any missing folders from that full path.
So if I already have C:\folder1 and it already has folder2, I want folder3 to be created.  When I pass the full path name, I dont know what folders, if any, already exist.

If I don’t have ANY of the folders, I want the entire path to be created.

I could pass \\server\directory\folder1\folder2\folder3 or U:\Reports\Sales\2007\
I can verify that the server/drive exists… but not how to recursively build the list of folders.

I can verify that there is no filename in that list… all I need is a function (or functions) that will make sure that the entire directory specified exists.  So by the time the function is done, I don’t have to check does folder exist or anything like that.

Thanks!
0
Comment
Question by:tfsaccount
  • 3
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34165992
This should do it:
path = "C:\test\folder\to\create"

Set fso = CreateObject("Scripting.FileSystemObject")
parts = Split(path, "\")
temp = parts(0) & "\"

For i = 1 To UBound(parts)
    temp = temp & parts(i) & "\"
    
    If Not fso.FolderExists(temp) Then
        fso.CreateFolder(temp)
    End If
Next

Open in new window

0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34166104

Simply call System.IO.Directory.CreateDirectory(directoryName) with the full path, it creates the directories for you!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34166127
@jasonduan

>>  Simply call System.IO.Directory.CreateDirectory(directoryName) with the full path,

In classic VB? I believe you're thinking of .NET  :)
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 11

Expert Comment

by:jasonduan
ID: 34166150
sorry, i was thinking of .net.
0
 
LVL 1

Author Closing Comment

by:tfsaccount
ID: 34172202
Thanks!!!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34172451
NP. Glad to help  :)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 85
Spell Check in VB6 13 137
Child Form in front 4 57
VBA: loop recent folder and copy txt file. 8 51
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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