Solved

# How to recursively add missing directories

Posted on 2010-11-18
302 Views
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
Question by:tfsaccount
• 3
• 2

LVL 75

Accepted Solution

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

0

LVL 11

Expert Comment

ID: 34166104

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

LVL 75

Expert Comment

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

LVL 11

Expert Comment

ID: 34166150
sorry, i was thinking of .net.
0

LVL 1

Author Closing Comment

ID: 34172202
Thanks!!!
0

LVL 75

Expert Comment

ID: 34172451
0

## Featured Post

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…