determine whether a path is a directory or a file

T

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
 
A

Anthony Jones

topramen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top