[thelist] With ASP's FileSystem Object, tell if directory is empty.

Chris Blessing webguy at mail.rit.edu
Tue Jul 23 09:20:01 CDT 2002


Craig-

You could check the .count property of the files collection:

<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.GetFolder(server.mappath("temp\"))

if fname.files.count > 0 then
	' files exist
	' list em, or do whatever
else
	' no files in this folder
	response.write "No files found."
end if

set fname=nothing
set fs=nothing
%>

You might also want to do some error checking to make sure the path
exists... like:

<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
fpath = server.mappath("tempasda\")

if fs.FileExists(fpath) then
	set fname=fs.GetFolder(fpath)

	if fname.files.count > 0 then
		' files exist
		' list em, or do whatever
	else
		' no files in this folder
		response.write "No files found."
	end if
else
	response.write "Invalid path."
end if

set fname=nothing
set fs=nothing
%>

HTH!

Chris Blessing
webguy at mail.rit.edu
http://www.330i.net

> Hi all,
>
> I've got the code below which lists all the files in a directory.  What I
> don't know how to do is if the directory is empty, to simply say no files
> were found.  I'm puzzled (doesn't take much :) ).
>
> <%
> set fs=Server.CreateObject("Scripting.FileSystemObject")
> set fname=fs.GetFolder(server.mappath("temp\"))
> for each x in fname.files
>   response.write x.name & "<br>"
> next
> set fname=nothing
> set fs=nothing
> %>
>
> Any help gladly appreciated.
>
> Regards,
> Craig.




More information about the thelist mailing list