Create Directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sueb
    Contributor
    • Apr 2010
    • 379

    Create Directory

    Wow, that was great! Now (and I guess I should have included this in my original question), can I create the "invalid" directory?

    ** Edit **
    Split from Dynamic hyperlinks.
    Last edited by NeoPa; May 19 '10, 10:46 PM. Reason: Added link data.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    You can use mkdir (makedir) to make new directorys. Be advised though, that you can only create 1 dir at a time using that.For example:You can only create C:\test\test2 if c:\test allready exists. The usage goes:

    Code:
    mkDir "C:\Test"
    Here is also a more advanced function that will loop through, and create the parent directories if they do not allready exist:
    Code:
    Public Function MakeDir(ByVal strPath As String) As Boolean
        'creates a directory independent of whether the parent directory exists
        'Code by TheSmileyOne
        'Version 0.1
        'Date 2010-05-19
        
        'Known issues
        'No error handling for cases such as network drives, with restricted permissions to create folders.
        'No input validation
        
        
        On Error GoTo err_Handler
        
        'Check if rightmost char is a \
            If Right(strPath, 1) = "\" Then
                'Strip it
                strPath = Left(strPath, Len(strPath) - 1)
            End If
        
        'Check if each individual directory exists, and if not, create it
            Dim strSplitPath() As String
            ReDim strSplitPath(UBound(Split(strPath, "\")))
            strSplitPath = Split(strPath, "\")
            Dim intI As Integer
            Dim strCombined As String
    
            For intI = 0 To UBound(strSplitPath)
                If intI <> 0 Then strCombined = strCombined & "\"
            
                strCombined = strCombined & strSplitPath(intI)
                If Dir(strCombined, vbDirectory) = "" Then
                    MkDir strCombined
                End If
            Next
        
         'Succes
             MakeDir = True
    Exit Function
            MakeDir = False
            MsgBox "Error " & Err.Number & " occured." & vbNewLine & Err.Description
        
    End Function

    Comment

    • sueb
      Contributor
      • Apr 2010
      • 379

      #3
      Originally posted by TheSmileyOne
      You can use mkdir (makedir) to make new directorys. Be advised though, that you can only create 1 dir at a time using that.For example:You can only create C:\test\test2 if c:\test allready exists. The usage goes:

      Code:
      mkDir "C:\Test"
      Here is also a more advanced function that will loop through, and create the parent directories if they do not allready exist:
      Code:
      Public Function MakeDir(ByVal strPath As String) As Boolean
          'creates a directory independent of whether the parent directory exists
          'Code by TheSmileyOne
          'Version 0.1
          'Date 2010-05-19
          
          'Known issues
          'No error handling for cases such as network drives, with restricted permissions to create folders.
          'No input validation
          
          
          On Error GoTo err_Handler
          
          'Check if rightmost char is a \
              If Right(strPath, 1) = "\" Then
                  'Strip it
                  strPath = Left(strPath, Len(strPath) - 1)
              End If
          
          'Check if each individual directory exists, and if not, create it
              Dim strSplitPath() As String
              ReDim strSplitPath(UBound(Split(strPath, "\")))
              strSplitPath = Split(strPath, "\")
              Dim intI As Integer
              Dim strCombined As String
      
              For intI = 0 To UBound(strSplitPath)
                  If intI <> 0 Then strCombined = strCombined & "\"
              
                  strCombined = strCombined & strSplitPath(intI)
                  If Dir(strCombined, vbDirectory) = "" Then
                      MkDir strCombined
                  End If
              Next
          
           'Succes
               MakeDir = True
      Exit Function
              MakeDir = False
              MsgBox "Error " & Err.Number & " occured." & vbNewLine & Err.Description
          
      End Function
      Smiley, this is wonderful! (In my code, I left in ALL the comment lines, including your authorship!) Now, I really do wish I'd asked this as a separate question so that I could mark this as "best" for this issue--thanks again!

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        Don't worry about the Best Answer thing. This is my hobby, sorting small VBA challenges to me, is like I guess/imagine Sudoko is for other people.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32656

          #5
          ADezii, being ADezii, asked if I would change the Best Answer (generous to a fault that boy) of the original thread. As this would not have been a best answer matching the question I could not do so (even at his request), but I was able to set it for this subsequent question as long as I split it into a separate thread (and I was happy to).

          Comment

          • sueb
            Contributor
            • Apr 2010
            • 379

            #6
            Originally posted by NeoPa
            ADezii, being ADezii, asked if I would change the Best Answer (generous to a fault that boy) of the original thread. As this would not have been a best answer matching the question I could not do so (even at his request), but I was able to set it for this subsequent question as long as I split it into a separate thread (and I was happy to).
            I am so grateful to the contributors of this site--without you, this task would have taken so much longer and been so much more painful (not that I'm finished with it even yet!). But I am also so please to have found a community of such generosity and professionalism . It's been a long time since I've had the pleasure to bask in that kind of glow. I only hope that one day I'll be able to contribute something meaningful to a discussion.

            Thanks again.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32656

              #7
              Don't underestimate your beautifully expressed thanks Sue.

              I know from my own experience how the boys will feel after working on this for you.

              You take care, and come back and visit any time you fancy.

              Comment

              Working...