• Search if directory exists – syntax

    Author
    Topic
    #351884

    Hi ya

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

    Thanks in advance
    Diana

    Viewing 0 reply threads
    Author
    Replies
    • #510430

      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

      • #510443

        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?

        • #510454

          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

          • #510455

            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.

        • #511283

          Geoff,

          also works as a stand alone VBS executed via the WSHost by changing syntax to:

          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

          the

          objShell.Popup

          is more flexible than

          MsgBox

          , but the latter works.

        • #511375

          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.

          • #511385

            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!

          • #511389

            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)

            • #511447

              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.

    Viewing 0 reply threads
    Reply To: Search if directory exists – syntax

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

    Your information: