|
-
Jun 14th, 2008, 10:15 PM
#1
Thread Starter
Lively Member
mkdir problems
You guys helped me a lot yesterday and got my questions answered in less than an hour.
I need more help though. I am having issues with mkdir... I can't make a whole directory, just pieces at a time. Here is an example:
instead of saying:
Code:
mkdir ("people\places\things\categories")
I have to say:
Code:
mkdir ("people\")
mkdir ("people\places\")
mkdir ("people\places\things\")
mkdir ("people\places\things\categories")
I think it should be able to do it all in one swing but I'm not sure. Can anyone provide a solution or correct me? Thanks!
-
Jun 14th, 2008, 10:19 PM
#2
Re: mkdir problems
Try this. There might be an API function for this but none that I know of.
Code:
Option Explicit
Private Sub Form_Load()
MkDirTree "C:\test3\test4\test5\test6"
End Sub
Private Sub MkDirTree(ByRef Path As String)
Dim s() As String, i As Integer, u As Integer
Dim strCurPath As String
s = Split(Path, "\")
u = UBound(s)
For i = 0 To u
strCurPath = strCurPath & s(i) & "\"
If Len(s(i)) > 0 And Len(Dir$(strCurPath)) = 0 Then 'Edited.
MkDir strCurPath
End If
Next i
Erase s
End Sub
Edit: Edited a line of code, make sure yours is same.
-
Jun 14th, 2008, 10:22 PM
#3
Thread Starter
Lively Member
Re: mkdir problems
 Originally Posted by DigiRev
Try this. There might be an API function for this but none that I know of.
Code:
Option Explicit
Private Sub Form_Load()
MkDirTree "C:\test3\test4\test5\test6"
End Sub
Private Sub MkDirTree(ByRef Path As String)
Dim s() As String, i As Integer, u As Integer
Dim strCurPath As String
s = Split(Path, "\")
u = UBound(s)
For i = 0 To u
strCurPath = strCurPath & s(i) & "\"
If Len(s(i)) > 0 And Len(Dir$(strCurPath)) = 0 Then 'Edited.
MkDir strCurPath
End If
Next i
Erase s
End Sub
Edit: Edited a line of code, make sure yours is same.
Thanks, that was an extremely fast response. I'll give it a try right now.
-
Jun 14th, 2008, 10:24 PM
#4
Thread Starter
Lively Member
Re: mkdir problems
This may be a double post, but I am excited! It worked well. You guys are fast and really nice. Not like the immature members of other forums I have used. Thank you so much!
-
Jun 14th, 2008, 10:30 PM
#5
Re: mkdir problems
You're welcome.
-
Jun 14th, 2008, 11:10 PM
#6
Thread Starter
Lively Member
Re: mkdir problems
actually, when I tried to use this with a variable that was a portion of the directory... it doesn't work I don't think.
MkDirTree (VARDIR & "\folder\another\another\")
Is this a limitation or is my coding probably off?
-
Jun 15th, 2008, 01:31 AM
#7
Re: mkdir problems
Heres a couple other ways,
Code:
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
(ByVal lpPath As String) As Long
' example usage
Dim path As String
path = "c:\test0\test1\test2\test3\test4"
MakeSureDirectoryPathExists path
Code:
Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" _
(ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
' example usage
Dim path As String
path = "c:\test0\test1\test2\test3\test4"
SHCreateDirectoryEx Me.hwnd, path, ByVal 0&
-
Jun 15th, 2008, 04:50 AM
#8
Thread Starter
Lively Member
Re: mkdir problems
 Originally Posted by Edgemeal
Heres a couple other ways,
Code:
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
(ByVal lpPath As String) As Long
' example usage
Dim path As String
path = "c:\test0\test1\test2\test3\test4"
MakeSureDirectoryPathExists path
Code:
Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" _
(ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
' example usage
Dim path As String
path = "c:\test0\test1\test2\test3\test4"
SHCreateDirectoryEx Me.hwnd, path, ByVal 0&
Thanks, I'll play with that soon. Hopefully that wwill allow me to use variables too
-
Jun 15th, 2008, 06:45 AM
#9
Re: mkdir problems
The MakeSureDirectoryPathExists API call is a favorite of mine. Credit to Hack for first bringing it to my attention. I'd recommend it over the other API suggested because it doesn't require an hwnd.
It is worth pointing out that when you use it, your path must end with a "\". So here's a nice wrapper function for you:
Code:
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
Public Sub CreateFolder(ByVal pstrFolder As String)
If Right$(pstrFolder, 1) <> "\" Then pstrFolder = pstrFolder & "\"
MakeSureDirectoryPathExists pstrFolder
End Sub
-
Jun 15th, 2008, 07:47 AM
#10
Re: mkdir problems
 Originally Posted by Ellis Dee
The MakeSureDirectoryPathExists API call is a favorite of mine. Credit to Hack for first bringing it to my attention. I'd recommend it over the other API suggested because it doesn't require an hwnd.
It is worth pointing out that when you use it, your path must end with a "\".
Right, my bad, I copied the path string from the other function, looking in my code bank I see there is a backslash at the end. Funny MSDN doesn't even mention that!
-
Jun 15th, 2008, 08:40 AM
#11
Thread Starter
Lively Member
Re: mkdir problems
 Originally Posted by Ellis Dee
The MakeSureDirectoryPathExists API call is a favorite of mine. Credit to Hack for first bringing it to my attention. I'd recommend it over the other API suggested because it doesn't require an hwnd.
It is worth pointing out that when you use it, your path must end with a "\". So here's a nice wrapper function for you:
Code:
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
Public Sub CreateFolder(ByVal pstrFolder As String)
If Right$(pstrFolder, 1) <> "\" Then pstrFolder = pstrFolder & "\"
MakeSureDirectoryPathExists pstrFolder
End Sub
I'm sorry but that doesn't make any sense to me because I'm pretty noobish and I don't really know much about API's how do I use that to create the directory? thanks for the help btw.
-
Jun 15th, 2008, 08:42 AM
#12
Re: mkdir problems
Copy that code to a bas module and you can use it from anywhere in your project:
CreateFolder "C:\people\places\things\categories"
-
Jun 15th, 2008, 09:08 AM
#13
Thread Starter
Lively Member
Re: mkdir problems
 Originally Posted by Ellis Dee
Copy that code to a bas module and you can use it from anywhere in your project:
CreateFolder "C:\people\places\things\categories"
Sounds easy enough. I'll give it a shot in a bit when I get the chance. Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|