determine whether a path is a directory or a file

Discussion in 'ASP General' started by topramen, Feb 1, 2007.

  1. topramen

    topramen Guest

    does any one here know of a good way to to determine whether or not a
    given path is a directory (e.g., "c:\mydir") or a file (e.g., "c:\mydir
    \myfile.txt")?

    i started attacking this problem by using filesystemobject to check
    whether or not a directory existed for the path. if so, it was a
    directory path. if not, i checked to see if a file existed for it. if
    so, it was a file path. this code is shown below:

    ''''''''''''''''''''''''''''''''''''
    function GetPathType(byval sPath)
    const INVALID = 0
    const DIRECTORY = 1
    const FILE = 2

    dim iResult : iResult = INVALID

    dim fso : set fso = CreateObject("Scripting.FileSystemObject")

    if fso.FolderExists(sPath) then
    iResult = DIRECTORY
    elseif fso.FileExists(sPath) then
    iResult = FILE
    end if

    set fso = nothing

    GetPathType = iResult
    end function
    ''''''''''''''''''''''''''''''''''''

    but, what if i need to determine a path type regardless of whether or
    not the directory or file currently exists?

    any ideas out there?

    thanks in advance
     
    topramen, Feb 1, 2007
    #1
    1. Advertising

  2. "topramen" <> wrote in message
    news:...
    > does any one here know of a good way to to determine whether or not a
    > given path is a directory (e.g., "c:\mydir") or a file (e.g., "c:\mydir
    > \myfile.txt")?
    >
    > i started attacking this problem by using filesystemobject to check
    > whether or not a directory existed for the path. if so, it was a
    > directory path. if not, i checked to see if a file existed for it. if
    > so, it was a file path. this code is shown below:
    >
    > ''''''''''''''''''''''''''''''''''''
    > function GetPathType(byval sPath)
    > const INVALID = 0
    > const DIRECTORY = 1
    > const FILE = 2
    >
    > dim iResult : iResult = INVALID
    >
    > dim fso : set fso = CreateObject("Scripting.FileSystemObject")
    >
    > if fso.FolderExists(sPath) then
    > iResult = DIRECTORY
    > elseif fso.FileExists(sPath) then
    > iResult = FILE
    > end if
    >
    > set fso = nothing
    >
    > GetPathType = iResult
    > end function
    > ''''''''''''''''''''''''''''''''''''
    >
    > but, what if i need to determine a path type regardless of whether or
    > not the directory or file currently exists?
    >
    > any ideas out there?
    >


    Not strictly possible.

    A file with no extension would as a string look identical to a folder path.
    Also a dot in a folder name is not illegal.

    However it's reasonable to assume the file will have a short extension
    hence:-

    Function ProbablyAFile(sPath)
    Dim rgx : Set rgx = New RegExp
    rgx.Pattern = "\.[^\\]{,5}$"
    ProbablyAFile = rgx.Test(sPath)
    End Function
     
    Anthony Jones, Feb 1, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Sway
    Replies:
    1
    Views:
    390
    William Ryan
    Aug 30, 2003
  2. Ben Fidge
    Replies:
    1
    Views:
    409
    Patrick Steele [MVP]
    Jun 22, 2004
  3. flipdoubt
    Replies:
    0
    Views:
    439
    flipdoubt
    Sep 3, 2005
  4. OvErboRed
    Replies:
    2
    Views:
    596
    FeU Hagen
    May 22, 2004
  5. Donjuan
    Replies:
    12
    Views:
    177
    Mosley Jones III
    Jul 25, 2003
Loading...

Share This Page