Directory Exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DThreadgill
    New Member
    • Aug 2007
    • 57

    Directory Exists

    Hi again - I've been using the following code to see if a directory exists and if it doesn't, it creates it:

    [CODE=vb]Dim isTrue As Boolean
    isTrue = DirExist(strPat h)
    If isTrue Then
    'do nothing
    Else
    DirCreateNested (strPath)
    End If
    strfilename = strPath & "\" & Mid(oRs![Account], 5) & ".doc"[/CODE]

    strpath is specified in the earlier part of the code as

    Code:
    strpath = I:\Folder\Subfolder\template.doc
    There are several people using the database and of course their drives are mapped differently. I wanted to use the network path in place of the drive letter (so there's only one database out there) but I get an error message stating that the path isn't found.

    Does the network path not work with this function or do I need to do something different?
    Last edited by DThreadgill; Apr 1 '08, 07:28 PM. Reason: Didn't get to finish question
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    I couldn't find any reference to DirExist() in my version (only 2000 at work I'm afraid).

    Maybe this code can help you sort out a replacement :
    Code:
    'Exist returns true if strFile exists.
    '22/05/2003 Rewritten with better code.
    '20/05/2005 Added finding of R/O, System & Hidden files
    Public Function Exist(strFile As String, _
                          Optional intAttrib As Integer = &H7) As Boolean
        Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
    End Function
    I can confirm it will work with UNC names. See below for results from my test.
    Code:
    ?Exist("\\STP01NTD001\Messaging\",&h17)
    True
    PS. Directories are not recognised as such if there is no trailing "\" character.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      You can find this in the help for Dir() but it certainly helps to understand the Attributes parameter.
      The attributes argument settings are:
      Code:
      [U]Constant  Value Description[/U]
      [B]vbNormal [/B]     0 (Default) Specifies files with no attributes. 
      [B]vbReadOnly [/B]   1 Specifies read-only files in addition to files with no attributes. 
      [B]vbHidden [/B]     2 Specifies hidden files in addition to files with no attributes. 
      [B]vbSystem [/B]     4 Specifies system files in addition to files with no attributes. Not available on the Macintosh. 
      [B]vbVolume [/B]     8 Specifies volume label; if any other attributed is specified, vbVolume is ignored. Not available on the Macintosh. 
      [B]vbDirectory [/B] 16 Specifies directories or folders in addition to files with no attributes. 
      [B]vbAlias [/B]     64 Specified file name is an alias. Available only on the Macintosh.

      Comment

      • DThreadgill
        New Member
        • Aug 2007
        • 57

        #4
        NeoPa - this worked great! I modified just a bit to coincide with what I already had:

        Code:
        Public Function Exist(strpath As String, _
                              Optional intAttrib As Integer = &H7) As Boolean
            Exist = (Dir(PathName:=strpath, Attributes:=intAttrib) <> "")
            If Exist = False Then
            DirCreateNested (strpath)
            Else
            End If
        End Function
        Along with this:
        Code:
        Public Function DirCreateNested(dPath As String) As Boolean
        
            On Error GoTo ErrHandler
        
           Dim dNr As Integer, cNr As Integer
           ' Set first char
           cNr = 1
           ' If last Char isn't \ then append it
           If Right(dPath, 1) <> "\" Then dPath = dPath & "\"
        
           Do
              dNr = InStr(cNr, dPath, "\", vbTextCompare)
              If dNr >= cNr Then
                      If DirExist(Left(dPath, dNr)) = False Then
                    ' Create the dir
                    MkDir (Left(dPath, dNr))
                 End If
                 ' Check if it's the last char and exit if true
                 cNr = dNr + 1: If cNr >= Len(dPath) Then Exit Do
              End If
           Loop
        
           DirCreateNested = True
           Exit Function
        
        ErrHandler:
           DirCreateNested = False
        
        End Function
        Works great! Thank you very much!!!
        & thanks for the Dir Attributes - I've filed away for future use!
        Last edited by DThreadgill; Apr 2 '08, 01:56 PM. Reason: Append

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          No worries. Very glad it helped.

          Just a quick suggestion though, lines 4 through 7 of your updated procedure could be rewritten as :
          Code:
          If Not Exist Then Call DirCreateNested(strpath)

          Comment

          • DThreadgill
            New Member
            • Aug 2007
            • 57

            #6
            Thanks for all the help in this guys - I'm currently having the users test the changes to the application and we're getting stuck at this create directory thing. It won't create the directories needed and I've stepped through this for the past 2 days and can't find anything. I might add that if the path is changed from UNC to N:\\..., it works, so I'm stumped.

            Code:
            Dim isTrue As Boolean
              isTrue = Exist(strpath)
            Code:
            Public Function Exist(strFile As String, _
                                  Optional intAttrib As Integer = &H7) As Boolean
                Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
            If Not Exist Then Call DirCreateNested(strFile)    
            End Function
            Code:
            Public Function DirCreateNested(dPath As String) As Boolean
               On Error GoTo ErrHandler
            
               Dim dNr As Integer, cNr As Integer
               ' Set first char
               cNr = 1
               ' If last Char isn't \ then append it
               If Right(dPath, 1) <> "\" Then dPath = dPath & "\"
            
               Do
                  dNr = InStr(cNr, dPath, "\", vbTextCompare)
                  If dNr >= cNr Then
                     If DirExist(Left(dPath, dNr)) = False Then
                        ' Create the dir
                        MkDir (Left(dPath, dNr))
                     End If
                     ' Check if it's the last char and exit if true
                     cNr = dNr + 1: If cNr >= Len(dPath) Then Exit Do
                  End If
               Loop
               DirCreateNested = True
               Exit Function
            
            ErrHandler:
               DirCreateNested = False
            
            End Function
            I did the ?Exist("\\myfil epath\",&h17) in the immediate window and it came up true even though the path wasn't created.

            Any additional help is greatly appreciated!!

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Originally posted by DThreadgill
              ...
              I did the ?Exist("\\myfil epath\",&h17) in the immediate window and it came up true even though the path wasn't created.
              I'm assuming from this that you mean the path wasn't created even after running your code, rather than that the Exist() function is returning an invalid result.

              I will progress from there, but I would suggest the folder creation call (DirCreateNested (strFile)) should logically be OUTSIDE of the Exist() function.

              Originally posted by DThreadgill
              Thanks for all the help in this guys - I'm currently having the users test the changes to the application and we're getting stuck at this create directory thing. It won't create the directories needed and I've stepped through this for the past 2 days and can't find anything. I might add that if the path is changed from UNC to N:\\..., it works, so I'm stumped.
              ...
              If you've stepped through the code I'd be interested to find out where the execution of the code (and the results thereof) differ from what you would have expected.

              Comment

              • DThreadgill
                New Member
                • Aug 2007
                • 57

                #8
                NeoPa

                I am using the DirCreateNested function and am getting different results depending on which path is being used. Here are the results that I'm getting when I step through:

                Using network path (N:\data\data\C ard\directory\s ubdirectory)
                as strpath:
                Code:
                Dim isTrue As Boolean
                  isTrue = DirExist(strpath)   'goes to DirExist(strpath)
                  If isTrue Then                    'do nothing
                  Else
                    DirCreateNested (strpath)     'goes to DirCreatedNested (strpath)
                  End If
                Code:
                Public Function DirExist(dPath As String) As Boolean
                   ' This function will test if a directory exist
                   ' If it does True is returned. Otherwise False
                   ' is returned.
                If Dir(dPath, vbDirectory) <> "" Then        'Dir(dpath, vbDirectory) = ""    
                      DirExist = True
                   Else
                      DirExist = False                              'so     DirExist = False
                   End If
                   
                   
                End Function
                Code:
                Public Function DirCreateNested(dPath As String) As Boolean
                
                   ' This function will create a nested directory
                   ' ie c:\Dir1\Dir2\Dir3. Returns True if dirs
                   ' are created successfully otherwise False.
                
                   On Error GoTo ErrHandler
                
                   Dim dNr As Integer, cNr As Integer
                   ' Set first char
                   cNr = 1
                   ' If last Char isn't \ then append it
                   If Right(dPath, 1) <> "\" Then dPath = dPath & "\"
                
                   Do
                      dNr = InStr(cNr, dPath, "\", vbTextCompare)                 'dNr = 3
                      If dNr >= cNr Then
                         If DirExist(Left(dPath, dNr)) = False Then          '=False - goes to DirExist
                            ' Create the dir
                            MkDir (Left(dPath, dNr))
                         End If
                         ' Check if it's the last char and exit if true
                         cNr = dNr + 1: If cNr >= Len(dPath) Then Exit Do
                      End If
                   Loop
                
                   DirCreateNested = True
                   Exit Function
                
                ErrHandler:
                   DirCreateNested = False
                
                End Function
                Everything works ok here and the directories are created. However, if I use the UNC path of \\servername\da ta\data\Card\di rectory\subdire ctory as my strpath, the results are different in that:

                DirCreateNested : dNr = InStr(cNr, dPath, "\", vbTextCompare) 'dNr = 1
                DirExist: Dir(dpath, vbDirectory) = Autoexec.bat 'where is this pulling from?
                Since Dir(dPath, vbDirectory) <> "" then it's returning DirExist=True and not creating the directory.

                I guess I'm expecting the same results with a UNC path as the Mapped drive letter path in that both should create the directories in the same way.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  That all depends on the code used. If you have code that works on the "\" character then the string will certainly work differently between the "Drive:\Folder\ ..." and "\\Server\Share \Folder\..." forms. These forms clearly are not consistent as far as where the "\" chars are found.

                  I will try to go through the logic more thoroughly to see if I can find the precise problem.

                  Comment

                  • DThreadgill
                    New Member
                    • Aug 2007
                    • 57

                    #10
                    Yep - I was working on that part yesterday afternoon but had to leave before I could finish it out. I believe it to be somewhere in the instr(cNr, dpath, "\") - if cNr = 1, it's looking at the first position and counting chars until the first "\" which is 3 on a mapped letter path. I'm playing around with changing the cNr = 3 so it will start after the "\\" on a UNC path. I'll let you know of my results!

                    Thanks so much for walking through me on this!

                    Comment

                    • DThreadgill
                      New Member
                      • Aug 2007
                      • 57

                      #11
                      I found the issue!

                      Since this is a UNC path, we can only create a directory where we have access. In this case, I had to change cNr = 33 to get it to the point where a directory could be created.

                      If you've stepped through the code I'd be interested to find out where the execution of the code (and the results thereof) differ from what you would have expected.
                      NeoPa - If it hadn't been for this statement, I wouldn't have slowed down to to see the obvious! - Thank you!!!!

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32633

                        #12
                        For me, that's the standard way to debug (See Debugging in VBA) :) I'm so pleased that helped anyway.

                        Just to throw a spanner in the works - have you considered accessing the various elements in your path string using the Split() function instead?
                        Code:
                        Dim varArray As Variant
                        Dim intFirst As Integer
                        
                        varArray = Split(strPath, "\")
                        If varArray(0) = "" Then
                          'No drive letter
                          If varArray(1) = "" Then
                            'UNC
                            intFirst = 4
                          Else
                            'Assumes current drive 
                            intFirst = 1
                          End If
                        Else
                          'Drive letter present
                          intFirst = 1
                        End If
                        This looks long and complex but it's mostly illustrative. In actuality your code could be a lot simpler. The benefit is that each element of the path would be in successive elements of the array.

                        Comment

                        • DThreadgill
                          New Member
                          • Aug 2007
                          • 57

                          #13
                          I debugged until I couldn't see straight! I knew it would be something obvious but your statement led me to start working on that particular section of the code.

                          I will definitely give this a try! Thanks again for your help!
                          Last edited by DThreadgill; Apr 18 '08, 02:59 PM. Reason: Additional Details

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32633

                            #14
                            Originally posted by DThreadgill
                            I debugged until I couldn't see straight!
                            That's such fun isn't it. Especially when you can see that it's not working but you're B******d if you can see why :D Glad I could at least point in the right direction :)
                            Originally posted by DThreadgill
                            I will definitely give this a try! Thanks again for your help!
                            No worries. I find so many things nowadays that are better manipulated with Split() & Join() rather than some complicated string processing.

                            Anyway - Good luck.

                            Comment

                            Working...