By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

folder

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

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***
Jun 3 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Eugene Anthony wrote:
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.

--
Mike Brind

Jun 3 '06 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Jun 9 '06 #3

P: n/a
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.

--
Mike Brind

Eugene Anthony wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***


Jun 9 '06 #4

P: n/a
I got this :)

"Found the folder:-)"

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***
Jun 9 '06 #5

P: n/a
I tried and I got the following result:

Found the folder:-)"

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***
Jun 9 '06 #6

P: n/a

Eugene Anthony wrote:
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
%>

--
Mike Brind

Jun 9 '06 #7

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Jun 9 '06 #8

P: n/a

Eugene Anthony wrote:
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?

--
Mike Brind

Jun 9 '06 #9

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Jun 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.