One of the many nice features of the scripting file system object (scrun.dll)
is the create folder method. Equivalent to executing 'mkdir' or 'md' at the
command prompt, this gives you a nice object based way to create folders in code
- from any language that can instantiate a COM object.
As cool as it is, there is one weakness you should keep in mind - it can't
create a series of folders in one call. For example, let's say you're working on
a database backup utility and you've decided to create a folder for each
database that has two sub folders, one for log backups and one for full db
backups. All of the database folders would be contained in a folder called C:\Backup.
It would seem logical that you could do this:
Dim fso As Scripting.FileSystemObject Set fso = New Scripting.FileSystemObject fso.CreateFolder Folder "C:\backup\master\logs" fso.CreateFolder Folder "C:\backup\master\full" Set fso = Nothing |
But unless both the backup and master folders exist, the logs and full
folders will not be created. This means you need to check to make sure that the
higher level folders exist first, which can lead to something that looks like
this:
Dim fso As Scripting.FileSystemObject Set fso = New Scripting.FileSystemObject If fso.FolderExists(FolderName) = False Then fso.CreateFolder Folder "C:\backup" end if If fso.FolderExists(FolderName) = False Then fso.CreateFolder Folder "C:\backup\master" If fso.FolderExists(FolderName) = False Then fso.CreateFolder Folder "C:\backup\master\logs" fso.CreateFolder Folder "C:\backup\master\full"
end if Set fso = Nothing |
As you can see, if you have more than a couple levels you end up with quite a
bit of repetitive code. Worse yet, you have to do those checks every time (if
you're coding defensively as you should!). Wouldn't it be nice to just call
CreateFolder and not worry about it?
The way to do that is to create your own improved version of CreateFolder
that is generic enough to handle any number of levels and does the checks for
intermediate folders automatically. The best way to do this is to use something
that is sometimes hard to follow, but so useful once you get the hang of it -
recursion! Any time you're processing a tree like structure (outlines, treeview
control, XML, org charts, etc), you're probably going to need a recursive
solution.
Recursion isn't the easiest thing to learn or to explain - my favorite saying
about recursion is that "it's like a snake eating it's tail". For each
branch of the tree, the recursive function (or sub) calls itself. You really
have to step through the code a few times to get a good feel for how it works.
The code below will create a folder(s) using recursion - load it into VB (or
Access or Excel even) and step through it - try to create a complex folder like c:\top\row1\row2\row3\row4\row5.
If you're using VBScript, you'll have to change New to CreateObject and remove
the 'as datatype' portions.
Sub CreateFolder (FolderName as string) Dim fso As Scripting.FileSystemObject Dim iBreak As Integer On Error Resume Next 'search from right to find path iBreak = InStrRev(FolderName, "\") If iBreak > 0 Then Call CreateFolder(Left$(FolderName, iBreak - 1)) End If Set fso = New Scripting.FileSystemObject If fso.FolderExists(FolderName) = False Then fso.CreateFolder FolderName End If Set fso = Nothing End Sub |
I think you'll be surprised how often you
can apply this once you're comfortable with the concept. Even if you don't want
to learn about recursion, the code is pretty useful anyway!