determine whether a path is a directory or a file
am 01.02.2007 20:23:19 von topramen
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
Re: determine whether a path is a directory or a file
am 02.02.2007 00:28:13 von Anthony Jones
"topramen" wrote in message
news:1170357799.671188.6440@h3g2000cwc.googlegroups.com...
> 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