Results 1 to 8 of 8
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Does Direrctory Exist? (XL97;SR2)

    If the directory exists, why would oDir="." provided the directory C:TestDirectory is on your drive. I'm using the following code:

    Var1 = "C:TestDirectory"
    oDir = Dir(Var1,vbDirectory)

    Thanks,
    John

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Does Direrctory Exist? (XL97;SR2)

    "." is the current directory, ".." is the directory before (if i remember my DOS)
    Call DIR with NO path to get the other files.

    From the HELP

    <hr>You must specify pathname the first time you call the Dir function, or an error occurs. If you also specify file attributes, pathname must be included.

    Dir returns the first file name that matches pathname. To get any additional file names that match pathname, call Dir again with no arguments. When no more file names match, Dir returns a zero-length string (""). Once a zero-length string is returned, you must specify pathname in subsequent calls or an error occurs. You can change to a new pathname without retrieving all of the file names that match the current pathname. However, you can't call the Dir function recursively. Calling Dir with the vbDirectory attribute does not continually return subdirectories.<hr>

    Steve

  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does Direrctory Exist? (XL97;SR2)

    I'm not sure what you are asking. That code should return

    <pre>oDir = "TestDirectory"
    </pre>


    if the directory exists and

    <pre>oDir = ""
    </pre>


    if the directory does not exist.

    The directory named "." is a throwback to the old DOS file structure and always meant the current directory, and the directory named ".." meant the parent directory of the current directory. That should work, but I don't understand how it relates to your question.
    Legare Coleman

  4. #4
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does Direrctory Exist? (XL97;SR2)

    Legare,

    I too would have expected oDir to return TestDirectory but it did not just the "." which puzzled me. If you have not done so already, please try the code on your computer. Perhaps the code I have is incorrect.

    John

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Does Direrctory Exist? (XL97;SR2)

    I can confirm that the code returns "." if oDir is the current active directory.
    -John ... I float in liquid gardens
    UTC -7ąDS

  6. #6
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Does Direrctory Exist? (XL97;SR2)

    I could not confirm this in my XL97 with winXP or Win95. I only get the directory name never ".".

    How did you make the directory "active"?

    When I think of Active:
    I saved the current file to my "var1" directory. So now when i press file open that is the directory that opens, and if I create a new workbook and press saveAs that is the default.

    Even if I change var1 to my "default file directory" (from tools-options) I can only get it to list the directory, never ".".

    Steve

  7. #7
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does Direrctory Exist? (XL97;SR2)

    I copied your code and pasted it into my XL2K SP3 and it returns the directory name, even if the directory is the currently active directory.

    The test that I use is:

    <pre> If oDir <> "" Then
    MsgBox "Directory exists."
    Else
    MsgBox "Can't find directory"
    End If
    </pre>

    Legare Coleman

  8. #8
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Does Direrctory Exist? (XL97;SR2)

    I browsed to it in Explorer. My test was initially run in the immediates window, but

    Sub dirloc()
    MsgBox Dir("Cocuments and SettingsOwnerMy DocumentsExcelJohn", vbDirectory)
    End Sub

    also returns "." in the messagebox.

    In any case, If the OP's question is whether the directory exists or not, Legare's code is fine. The function that I usually use is from <!profile=Kevin>Kevin<!/profile>:

    Public Function FileExists(strPathFile) As Boolean
    FileExists = CBool(Len(Dir(strPathFile, vbDirectory)))
    End Function

    and I think it works for files or directories.
    -John ... I float in liquid gardens
    UTC -7ąDS

Posting Permissions

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