folder

E

Eugene Anthony

How do I calculate the size of a folder? Also how do I count the number
of images stored in a folder?

Eugene Anthony
 
M

Mike Brind

Eugene said:
How do I calculate the size of a folder? Also how do I count the number
of images stored in a folder?

<%
path = "path\to\folder"

Set fso = CreateObject("Scripting.FileSystemObject")
Set theCurrentFolder = fso.GetFolder(path)
Set curFiles = theCurrentFolder.Files

For Each fileItem in curFiles
fName = fileItem.Name
dotpos = instr(fName,".")
fExt = right(fname,len(fName)-dotpos)
If fExt = "jpg" or fExt = "gif" Then
fSize = fileItem.Size '<<
totalSize = totalSize + fSize '<<
totalFiles = totalFiles + 1
End If
Next

Response.Write totalSize & "<br />" 'in bytes
Response.Write totalFiles
%>

Not clear whether you only have images in your folder. This will only
count images and give a total size for all of them. If you want the
total size of the folder regardless of file type, move the lines
commented with '<< outside of the If...End If structure.
 
E

Eugene Anthony

I have tried this but it does not work. The response.write displays no
results.

<%

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

If objFSO.FolderExists(myFolderPath) Then
'The main picture folder exists
Set objPicturesFolder = objFSO.GetFolder(myFolderPath)
Set collPicturesFolders = objPicturesFolder.SubFolders
Set curFiles = collPicturesFolders.Files

For Each fileItem in curFiles
fSize = fileItem.Size
totalSize = totalSize + fSize
totalFiles = totalFiles + 1
Next

response.write totalFiles

%>

Eugene Anthony
 
M

Mike Brind

Did you check to see if the path you provided was correct?

If objFSO.FolderExists(myFolderPath) Then
'The main picture folder exists
Response.Write "Found the folder:)"

Try that, see what you get. If you get nothing, post your path
statement here.
 
E

Eugene Anthony

I tried and I got the following result:

Found the folder:)"

Eugene Anthony
 
M

Mike Brind

Eugene said:
I tried and I got the following result:

Found the folder:)"

Eugene Anthony
Yeah - sorry. My bad (I think that's the phrase...?). I should have
looked more closely at your code and would then have spotted you were
going through subfolders. When you do that, you need to iterate
through each subfolder in turn to get the file details:

<%
Set fso = CreateObject("Scripting.FileSystemObject")
Set objPicturesFolder = fso.GetFolder(path)
Set collPicturesFolders = objPicturesFolder.SubFolders
For Each subflder in objPicturesFolder.SubFolders
Response.Write "<strong>" & subflder.name & "</strong><br>"
Set curFiles = subflder.Files

For Each fileItem in curFiles
fName = fileItem.Name
dotpos = instr(fName,".")
fExt = right(fname,len(fName)-dotpos)
If fExt = "jpg" or fExt = "gif" Then
fSize = fileItem.Size '<<
totalSize = totalSize + fSize '<<
totalFiles = totalFiles + 1
Response.Write fName & "<br>"
End If
Next
Next

Response.Write totalSize & "<br />" 'in bytes
Response.Write totalFiles
%>
 
E

Eugene Anthony

This is the full code. I have tried the code but it does not work. This
is the code I have inserted:

<%
Set curFiles = indPicturesFolder.Files
For Each fileItem in curFiles
fSize = fileItem.Size
totalSize = totalSize + fSize
totalFiles = totalFiles + 1
Next
response.write totalFiles
%>


The full code:


<%Option Explicit%>
<!--#INCLUDE FILE="inc_Common.asp" -->
<%
On Error Resume Next

StartPosition = Request("imgCount")
myFolderPath = Server.MapPath(myFolder)
counter = 0
lImgCounter = 0
%>
<html>
<head>
<title>Photos</title>
</head>

<body bgcolor="#000000">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="420"
align="center">
<tr>
<td class="header" align="left" width="10"><img
src="icons/td_top_left.gif" width="8" height="20" border="0"></td>
<td class="header" align="center" width="400"><font
class="PopTitle">Photos</font></td>
<td class="header" align="right" width="10"><img
src="icons/td_top_right.gif" width="8" height="20" border="0"></td>
</tr>
</table>
<table cellpadding="3" cellspacing="0" border="0" width="420">
<tr>
<td width="120">&nbsp;</td>
<td width="300"><br>
<%
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(myFolderPath) Then

'The main picture folder exists
Set objPicturesFolder = objFSO.GetFolder(myFolderPath)
Set collPicturesFolders = objPicturesFolder.SubFolders

For Each indPicturesFolder in collPicturesFolders
indPicturesFolderSpaces = Replace(indPicturesFolder.Name,"_"," ")
If (CLng(counter) >= CLng(StartPosition)) and (Clng(counter) <
(CLng(StartPosition) + Clng(ImageLimit))) then
lImgCounter = lImgCounter + 1
%>


<%

Set curFiles = indPicturesFolder.Files

For Each fileItem in curFiles
fSize = fileItem.Size
totalSize = totalSize + fSize
totalFiles = totalFiles + 1
Next

response.write totalFiles

%>

<img src="icons/orangeball.gif" align="top">&nbsp;
<a href="thumb.asp?Folder=<%= indPicturesFolder.Name %>"
class="links">
<%= indPicturesFolderSpaces %></A><br><br>
<%
end if
counter = counter + 1
Next

ThumbNextLink = CLng(StartPosition) + Clng(lImgCounter)
ThumbPreviousLink = CLng(StartPosition) - Clng(ImageLimit)
%>
<%

Set collPicturesFolders = Nothing

Else
'The main picture folder does not exists
%>
<font class="error">No Pictures could be found.</font>
<%
End If
%>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="420"
align="center">
<tr>
<td class="middle" align="left" width="10"><img
src="icons/td_bot_left.gif" width="8" height="20" border="0"></td>
<% If (CLng(StartPosition) >= CLng(ImageLimit)) Then %>
<td class="middle" align="left" width="80">
<A href="Default.asp?imgCount=<%= ThumbPreviousLink %>"
class="links">Previous</A>
</td>
<% End If %>
<td class="middle" align="center" width="*">&nbsp;</td>
<% If (CLng(StartPosition)) < (CLng(counter) - (CLng(ImageLimit)))
Then %>
<td class="middle" align="right" width="55">
<A href="Default.asp?imgCount=<%= ThumbNextLink %>"
class="links">Next</A>
</td>
<% End If %>
<td class="middle" align="right" width="10"><img
src="icons/td_bot_right.gif" width="8" height="20" border="0"></td>
</tr>
</table>
</body>
</html>
<%
Set objFSO = Nothing
Response.Flush
%>


Eugene Anthony
 
M

Mike Brind

Eugene said:
This is the full code. I have tried the code but it does not work. This
is the code I have inserted:

<%
Set curFiles = indPicturesFolder.Files
For Each fileItem in curFiles
fSize = fileItem.Size
totalSize = totalSize + fSize
totalFiles = totalFiles + 1
Next
response.write totalFiles
%>


The full code:


<%Option Explicit%>
<!--#INCLUDE FILE="inc_Common.asp" -->
<%
On Error Resume Next

StartPosition = Request("imgCount")

OK. In order for you to get any understanding why this doesn't work,
the first thing you have to do is get rid of On Error Resume Next. The
next thing is to get rid of the black background for response.write
purposes. I, for one, always start out with the raw ASP code with no
html to clutter it up. Once I know this is working, then I add the
html.

I'll give you a clue to the first error. You have used Option Explicit
and not declared on variable explicitly.

What is Request("imgCount")? Is it Form or QueryString? Whichever it
is, don't use this shorthand. Does it have a value?
 
E

Eugene Anthony

It works now,

as u have mentioned about <%Option Explicit%> so I declare my variables
in <!--#INCLUDE FILE="inc_Common.asp" -->.

The other question that I have posted is a separate question in regards
to the image size.

Eugene Anthony
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top