Results 1 to 10 of 10
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search if directory exists - syntax

    Hi ya

    I need to check if a directory exists in a path.
    does anyone know the syntax?

    Thanks in advance
    Diana

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search if directory exists - syntax

    Diana,

    I've done it like this:

    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    If Not fs.folderexists("C:test") Then
    MsgBox "Folder not found"
    End If
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search if directory exists - syntax

    Whoops- I forgot to mention. The solution works on Word 2000 SR1. I think I used the same solution in O97- but I'm not quite sure.

    In what environment did you want to get the solution?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  4. #4
    Star Lounger
    Join Date
    Apr 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Look at help for

    Some sample code below:

    Dim strExists
    strExists = Dir(txtDirectory.Text, vbDirectory)

    If Len(strExists) > 0 Then
    MsgBox "Directory Exists", vbOKOnly, "Directory Exists"
    Else
    MsgBox "Directory Does NOT Exist", vbOKOnly, "Directory DOES NOT Exist"
    End If

  5. #5
    Star Lounger
    Join Date
    Apr 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Forgot Something

    The code sample I used was based on a form with a text box titled "txtDirectory" where the directory to be checked is entered. You should be able to substitute your variable's name for "txtDirectory.Text" in the code.

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search if directory exists - syntax

    Geoff,

    also works as a stand alone VBS executed via the WSHost by changing syntax to:
    <pre>Dim fs, fldr
    Set fs = CreateObject("Scripting.FileSystemObject")
    fldr=InputBox("Name of folder?", "Where am I?")
    If Not fs.folderexists(fldr) Then
    MsgBox "You are lost. " & fldr & " does not exist on this computer."
    Else
    MsgBox "Lucky you. " & fldr & " is a valid folder."
    End If</pre>


    the <pre>objShell.Popup</pre>

    is more flexible than <pre>MsgBox</pre>

    , but the latter works.

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Search if directory exists - syntax

    I don't think Office 97 supported the FileSystemObject object, although it seems to me that VB 5.0 did. Office 2000 supports it through a reference to the Office 9.0 object library.
    Charlotte

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Search if directory exists - syntax

    The following is a little mysterious, but on the "old" Windows98/Office 97 side of my dual-boot home PC, the FileSystemObject code runs OK.
    I do have IE5 on there.
    Part of the mystery is, I can see Microsoft Scripting Runtime in the list of available references, yet I don't have a reference to it, and the code still runs!

  9. #9
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search if directory exists - syntax

    Charlotte,

    Fair point.

    I'm not sure when scripting became available. When I run Office 97 it supports it, if I add a reference to Microsoft Scripting Runtime (SCRRUN.DLL). So if it was not included with O97 (I can't check) but was with VB5, then Word97 would have been able to access it.

    Which leads to the next potential problem. If you use scripting (or anything else where you add referecnes) you do need to make sure to distribute the DLL/OCX (provided you have the right to do so)
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  10. #10
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Search if directory exists - syntax

    It might very well have been available in 97 through the scripting library. I never tried that. For Access 2000, you can do late binding on the FileSystemObject with an Office 9.0 object library reference, but early binding still requires the scripting library.
    Charlotte

Posting Permissions

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