473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2541

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dfenestr8 | last post by:
os.listdir("path") returns the names of all the items in a directory ok, but I need to know whether the item is a file or a directory. Can anybody tell me how this could be done?
5
by: bart plessers | last post by:
Hello, Currently I am developping a internet "directory browser" My page 'default.asp' has following entries: CurrentPATH = Request("MyLink") Set oFSO =...
13
by: could ildg | last post by:
I want to check if a folder named "foldername" is empty. I use os.listdir(foldername)== to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do...
1
by: Tim Marsden | last post by:
HI If I have 2 strings each containing a Path string, but in different formats. e.g. 1. "D:\My Folder\My Sub Folder\A.txt" and 2. "\\ComputerA\Share B\A.txt" Now do I compare these to...
3
by: KSC | last post by:
Hello, Is there a way to programmatically determine if a directory is shared and if so, what the sharename is? It seems a simple question, but I have been searching and not found the...
28
by: Tim Daneliuk | last post by:
I have a program wherein I want one behavior when a file is set as executable and a different behavior if it is not. Is there a simple way to determine whether a given named file is executable...
2
by: Markus | last post by:
Hello Is it possible to determine the PHP path to the FTP base directory? ftp_pwd($conn_id) returns '/', while I need something like '/data/users/domain.com/'. I used a construct such as: ...
0
by: grejon04 | last post by:
I'm trying to write a script that will look into a directory, and check each entity to see if it is a directory or a file. (Borland CodeGear RAD Studio) Here's what I have so far. #include...
6
by: lawpoop | last post by:
I'm working on a PHP site and I have my database password file behind the server root directory. I can reference the password file by include("../ database_password.inc.php"), but I would like...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.