Results 1 to 13 of 13

Thread: mkdir problems

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    65

    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!

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    65

    Re: mkdir problems

    Quote 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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    65

    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!

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: mkdir problems

    You're welcome.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    65

    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?

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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&

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    65

    Re: mkdir problems

    Quote 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

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: mkdir problems

    Quote 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!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    65

    Re: mkdir problems

    Quote 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.

  12. #12
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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"

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    65

    Re: mkdir problems

    Quote 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
  •  



Click Here to Expand Forum to Full Width