Forum Moderators: open

Message Too Old, No Replies

Counting number of files in a folder.

         

traeanthony

2:23 am on Nov 13, 2005 (gmt 0)

10+ Year Member



I'm trying to find a way to get the number of files in a folder so I can have an image display on a web page when ever a file is added. I know how to display the image in an if statement, but actually counting the files is beyond me. I know it can be done becuase I've tried various coding snipets but it only displays the actual number of files using "response.write". I need something like the following (I know the first line isn't actual coding, it was just to help everyone figure out what I'm talking about.)

----

"num" = countfiles in directory "c:\folder"

<%if request("num") > "1" then%>
<img src="image.jpg">
<%end if%>

----

Any clues?

carguy84

5:08 am on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Get the folder object associated with the directory
Dim objFolder
Set objFolder = objFSO.GetFolder("C:\InetPub\wwwroot\folder")

'Loop through the Files collection
Dim objFile, fileCount
fileCount = 0
For Each objFile in objFolder.Files
if lcase(right(objFile.Name, 4)) = ".jpg" then
fileCount = fileCount + 1
end if
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing

This will count all JPGs, you can take out the if statement if you want to count all files, or adjust it as necessary,

Chip-

traeanthony

6:28 am on Nov 13, 2005 (gmt 0)

10+ Year Member



First of all, thank you so much for replying to this topic. So I want to count all the text files (.txt) so i've changed the .jpg to .txt. Now, if there are any txt files in that directory, I want an image to display on a web page. How would I accomplish that? I've tried to figure it out with your code, but I'm still a little lost.

I know the if state ment would be
<%if request("---") > "0" then%>
<img src="image.jpg">
<%end if%>

what would I replace "---" with?

carguy84

7:05 pm on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the if statement(placed below all the code I wrote above) would be:

<% if fileCount > 0 then %>
<img src="image.jpg">
<% end if %>

traeanthony

9:16 pm on Nov 13, 2005 (gmt 0)

10+ Year Member



Thank you so much. It works perfectly!