Solved

How to recursively add missing directories

Posted on 2010-11-18
6
299 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility

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

Expert Comment

by:käµfm³d 👽
Comment Utility
@jasonduan

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

In classic VB? I believe you're thinking of .NET  :)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 11

Expert Comment

by:jasonduan
Comment Utility
sorry, i was thinking of .net.
0
 
LVL 1

Author Closing Comment

by:tfsaccount
Comment Utility
Thanks!!!
0
 
LVL 74

Expert Comment

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

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.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 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

17 Experts available now in Live!

Get 1:1 Help Now