VB icon

Recursive Make Folder

Email
Submitted on: 6/3/2018 6:52:00 PM
By: Nicholas Forystek  
Level: Intermediate
User Rating: By 1 Users
Compatibility: VB 6.0
Views: 2983
author picture
 
     This function is a recursion code example of making a folder with no added global variables or trailing parameters, simply just the initial needed parameter of the path to the folder to create and it will make it and any other path it may need to make not existing down the depth of the directory structure. My own strict requirements before posting, when I all of a sudden needed, this ability was strictly that it be one function only. The result return is the path created (or already existing), otherwise an error will be raised.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Recursive Make Folder
' Description:This function is a recursion code example of making a folder with no added global variables or trailing parameters, simply just the initial needed parameter of the path to the folder to create and it will make it and any other path it may need to make not existing down the depth of the directory structure. My own strict requirements before posting, when I all of a sudden needed, this ability was strictly that it be one function only. The result return is the path created (or already existing), otherwise an error will be raised.
' By: Nicholas Forystek
'**************************************

Public Function MakeFolder(ByRef Path As String)
 On Error Resume Next
 If InStr(Path, "\") > 0 Then
GetAttr Left(Path, InStrRev(Path, "\") - 1)
If Err.Number = 76 Or Err.Number = 53 Then
 Err.Clear
 MakeFolder = Path
 Path = MakeFolder(Left(Path, InStrRev(Path, "\") - 1))
Else
 MakeFolder = Path
End If
 End If
 If Err.Number = 0 Then
GetAttr MakeFolder
If Err.Number = 76 Or Err.Number = 53 Then
 Err.Clear
 On Error GoTo -1
 MkDir MakeFolder
End If
 End If
End Function


Other 24 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/3/2018 6:35:02 AMRob C

A little tricky getting the paste right, but worth the effort, as appears to do what you say.
Gave it 5, thanks for sharing,
Rob
PS Do you have a library of your Folders & Files code to share ?
(If this comment was disrespectful, please report it.)

 
12/24/2018 2:01:10 AMJack

Hi
Have a look to the API named "MakeSureDirectoryPathExists" from imagehlp.dll libreary : it does the same with one ligne of code.
;-)
(If this comment was disrespectful, please report it.)

 
7/12/2019 3:39:37 AMRob C

The layout of the code above is not great.
If you click the 'paste friendly' link above it looks great, but is impossible to paste (it pastes, but not friendly)
I will see if my paste into this comment is more friendly -
Public Function MakeFolder(ByRef Path As String)
On Error Resume Next
If InStr(Path, "\") > 0 Then
GetAttr Left(Path, InStrRev(Path, "\") - 1)
If Err.Number = 76 Or Err.Number = 53 Then
Err.Clear
MakeFolder = Path
Path = MakeFolder(Left(Path, InStrRev(Path, "\") - 1))
Else
MakeFolder = Path
End If
End If
If Err.Number = 0 Then
GetAttr MakeFolder
If Err.Number = 76 Or Err.Number = 53 Then
Err.Clear
On Error GoTo -1
MkDir MakeFolder
End If
End If
End Function
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.