Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    May 2001
    Location
    indiana
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FolderExists (Windows XP SP2/VBA)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: FolderExists (Windows XP SP2/VBA)

    You could use good old Dir with a wildcard and vbDirectory for this:
    <code>
    Dim blnFolderExists As Boolean
    blnFolderExists = Not (Dir("F:BB123*", vbDirectory) = "")</code>

  3. #3
    2 Star Lounger
    Join Date
    May 2001
    Location
    indiana
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FolderExists (Windows XP SP2/VBA)

    Thanks Hans

    Worked like a charm!!

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FolderExists (Windows XP SP2/VBA)

    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

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: FolderExists (Windows XP SP2/VBA)

    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)?

    <pre>C:Testingempty>subst r: .

    C:Testingempty>subst
    R:: => C:Testingempty

    C:Testingempty>dir r:*.*
    Volume in drive R is <img src=/w3timages/censored.gif alt=censored border=0>
    Volume Serial Number is <img src=/w3timages/censored.gif alt=censored border=0>

    Directory of R:

    05/04/2005 05:42 PM <DIR> .
    05/04/2005 05:42 PM <DIR> ..
    0 File(s) 0 bytes
    2 Dir(s) 1,190,252,544 bytes free
    </pre>


  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: FolderExists (Windows XP SP2/VBA)

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

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: FolderExists (Windows XP SP2/VBA)

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

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FolderExists (Windows XP SP2/VBA)

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

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FolderExists (Windows XP SP2/VBA)

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

    <pre>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</pre>


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

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FolderExists (Windows XP SP2/VBA)

    FSO doesn't seem to fare much better:<pre>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
    </pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •