470,614 Members | 1,491 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

determine whether a path is a directory or a file

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

Feb 1 '07 #1
1 2408

"topramen" <ah***@oneirasoft.comwrote in message
news:11********************@h3g2000cwc.googlegroup s.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
Feb 1 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dfenestr8 | last post: by
13 posts views Thread by could ildg | last post: by
28 posts views Thread by Tim Daneliuk | last post: by
2 posts views Thread by Markus | last post: by
6 posts views Thread by lawpoop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.