• FolderExists (Windows XP SP2/VBA)

    Author
    Topic
    #415273

    I am using the FolderExist() Method, which works correctly as stated in the help file. I would however, if possible, like to use the method to search for a folder that starts with a certain string… For example, If a folder exists called “BB123 Building Project”, i would like to search for “BB123” and return back that the folder exists. Is there a way to do this?

    Thanks in Advance

    Viewing 0 reply threads
    Author
    Replies
    • #925658

      You could use good old Dir with a wildcard and vbDirectory for this:

      Dim blnFolderExists As Boolean
      blnFolderExists = Not (Dir("F:BB123*", vbDirectory) = "")

      • #925665

        Thanks Hans

        Worked like a charm!!

      • #945438

        Hmmmm. Interesting. I have set up a RamDrive “R:” to simulate a client’s network drive. All their drives are single letters.

        The DIR fails to detect the drive when there are no files yet created within R:.
        Once I copy a file to drive R:, I can detect the folder.
        Once I delete that file, the folder R: is undetectable.

        I’m using RamDrive.sys from KB 257405

        • #945503

          Maybe it is a flaw in the driver? A Dir of an empty drive should return the “self” folder (.) at least, and typically returns the “parent” folder (..) even if there really isn’t one. Can you test this out at a command prompt (you can skip making the SUBST since you already have a drive letter to test)?

          C:Testingempty>subst r: .
          
          C:Testingempty>subst
          R:: => C:Testingempty
          
          C:Testingempty>dir r:*.*
           Volume in drive R is  censored
           Volume Serial Number is  censored
          
           Directory of R:
          
          05/04/2005  05:42 PM              .
          05/04/2005  05:42 PM              ..
                         0 File(s)              0 bytes
                         2 Dir(s)   1,190,252,544 bytes free
          
          • #945506

            Chris’s post is about the VB/VBA function Dir, not about the command prompt instruction DIR.

            • #945508

              Yeah, that was lazy of me… The Immediate window does show the . and .. folders when you run this against a substituted drive:

              Sub ShowDirsInSubstDrive()
              Dim strFile As String
              strFile = Dir(“r:”, vbDirectory)
              While strFile vbNullString
              Debug.Print strFile
              strFile = Dir
              Wend
              End Sub

              That’s why I think there’s something fishy about the RAM drive driver… or maybe it’s something in Chris’s code…

          • #945510

            > Maybe it is a flaw in the driver?

            Despite Hans’s (thanks Hans!) counter and your counter-counter i think that you are right.

            Before I posted I considered that there was a software bug (well, it couldn’t be a hardware fault, now could it?!!!) and the RamDisk.sys or whatever, on finding no space set aside for an initial root folder merely reported a null string or similar, whereupon the VBA/DIR command reported a non-existent drive. I suspect that the ram disk root folder doesn’t get created/initialised until the first file is lodged there. As an interesting footnote, later on I dragged any old file in there, and it didn’t work untiul I cleared the Hidden attribute, so it seems as if it has to be a visible file in the ram drive before VBA/DIR reports success.

            I should have posted that my post was “for information only”, because if THIS client ever has an empty “R:” drive, they’ll be having more serious problems that my little bit of VBA code can offer.

      • #968531

        FWIW this does not appear to be a foolproof technique! (blush)
        For the second sample, according to my WinXP Command prompt “The filename, directory name, or volume label syntax is incorrect.”.

        Public Function blnFolderExists(strFolderName) As Boolean
            On Error Resume Next
            blnFolderExists = Not (Dir(strFolderName, vbDirectory) = "")
        End Function
            Sub TESTblnFolderExists()
                MsgBox blnFolderExists("C:") ' should return TRUE
                MsgBox blnFolderExists("C:") ' should return FALSE
            End Sub

        I discovered this after examining how my code was able to create a folder with multiple periods!

        • #968535

          FSO doesn’t seem to fare much better:

          Public Function blnNEWFolderExists(strPath As String)
              Dim objFSO  As Object
              Set objFSO = CreateObject("Scripting.FileSystemObject")
              blnNEWFolderExists = objFSO.FolderExists(strPath)
          End Function
          Sub TESTblnNEWFolderExists()
              MsgBox blnNEWFolderExists("c:")
              MsgBox blnNEWFolderExists("c:")
              MsgBox blnNEWFolderExists("M:G04liugh")
              MsgBox blnNEWFolderExists("M:G04liugh....")
          End Sub
          
    Viewing 0 reply threads
    Reply To: FolderExists (Windows XP SP2/VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: