Create directory paths

Want to create a directory on a file system? These 2 functions will do the trick. You must pass the complete path of the directory you want to create (ie. c:tempsub1sub2 ). It can be either a partial or completely non-existent path. Try it!!

 Sub CreateDirectory(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 sub-directories as needed ' By Giulio Campobassi, 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 Else 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) Else 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 Wend If Mid(strCurDir,Len(strCurDir),1) = "" Then strNewSubDir = strCurDir + strSubDir Else strNewSubDir = strCurDir + "" + strSubDir End If strCurDir = strNewSubDir If Not iSubDirFound Then Mkdir(strNewSubDir) iDirCreated = True Else strFName = Dir$(strCurDir + "*.*", 16) End If Else If Trim(strSubDir) <> "" Then strNewSubDir = strCurDir + "" + strSubDir Mkdir(strNewSubDir) strCurDir = strNewSubDir Else continue = False End If End If iEndPosCurDir = Len(strCurDir) Wend End Sub Function getSubDir(strFullPATH As String, iStartPos As Integer) As String Dim iEndPos As Integer Dim iSubDirLength As Integer getSubDir = "" If iStartPos < Len(strFullPath) Then iEndPos = Instr(iStartPos, strFullPath, "") If iEndPos > 0 Then iSubDirLength = iEndPos - iStartPos getSubDir = Mid$(strFullPath, iStartPos, iSubDirLength) End If End If End Function

This was first published in April 2001

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.