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

How to recursively add missing directories

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
tfsaccount
Asked:
tfsaccount
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
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
 
jasonduanCommented:

Simply call System.IO.Directory.CreateDirectory(directoryName) with the full path, it creates the directories for you!
0
 
käµfm³d 👽Commented:
@jasonduan

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

In classic VB? I believe you're thinking of .NET  :)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jasonduanCommented:
sorry, i was thinking of .net.
0
 
tfsaccountAuthor Commented:
Thanks!!!
0
 
käµfm³d 👽Commented:
NP. Glad to help  :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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