Directory Generator Function (This One Really Works!)

There have been a few attempts in the past to write a directory generator in the past. Most of them are too limited or just don't work.

This little bit of code lets you pass a string (which is a directory path from the root) and will generate all the directories as needed. It can handle partial or non-existent directory paths. Please try it. Please Submit any improvements to me.


Sub CheckForCreateDirectory(FullPath As String)
' Given a complete directory path including the root directory, (eg h:wordtemplatesfaxes), this function will create all directories in a path as requried.
' Also, if a partial path exists it will follow it and create subdirectories as needed.
'By Giulio Campobassi, ASIC, giulio.campobassi@asic.gov.au, October 2000.

Dim iStartPos As Integer
Dim iEndPosCurDir As Integer
Dim iEndPosSubDir As Integer
Dim strSubDir As String
Dim strCurDir As String
Dim strFName As String
Dim strNewSubDir As String
Dim iSubDirFound As Integer
Dim iSubDirLength As Integer
Dim iDirCreated As Integer
Dim iOffset As Integer
Dim Continue As Integer

iStartPos = 1
' Start at the root
iEndPosCurDir = Instr(FullPath, "")
strCurDir = Mid$(FullPath, iStartPos, iEndPosCurDir)
iDirCreated = False
' get all the files in the starting directory
strFName = Dir(strCurDir,16)
continue = True
While strFName <> "" Or continue
iSubDirFound = False
If Mid(FullPath, iEndPosCurDir + 1,1) = "" Then
iOffset = 2
iOffset = 1
End If
' get the subdirectory
strSubDir = getSubDir(FullPath, iEndPosCurDir + iOffset)
If Trim(strSubDir) = "" Then
' if at the end of the list then exit
If iEndPosCurDir < Len(fullpath) Then
strSubDir = Mid(FullPath, iEndPosCurDir + iOffset, Len(fullPath) - iEndPosCurDir + iOffset)
Exit Sub
End If
End If
' Branch here if a directory has been created at any time
If Not iDirCreated Then
While strFName <> "" And Not iSubDirFound
If Ucase(strFName) = Ucase(strSubDir) Then
iSubDirFound = True
End If
If Not iSubDirFound Then
strFName = Dir$()
End If
If Mid(strCurDir,Len(strCurDir),1) = "" Then
strNewSubDir = strCurDir + strSubDir
strNewSubDir = strCurDir + "" + strSubDir
End If

strCurDir = strNewSubDir
If Not iSubDirFound Then
iDirCreated = True
strFName = Dir$(strCurDir + "*.*", 16)
End If

If Trim(strSubDir) <> "" Then
strNewSubDir = strCurDir + "" + strSubDir
strCurDir = strNewSubDir
continue = False
End If
End If
iEndPosCurDir = Len(strCurDir)
End Sub


This was first published in November 2000

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.