Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan Subfolders within subfolders 1

Status
Not open for further replies.

Aqif

Programmer
Joined
Apr 27, 2002
Messages
240
Location
AU
Hi

I wrote a code which scans subfolders and return the folder name and sizes. But the code is only going to 1st level of subfolders. is there anyway I can modify to go to indefinite levels.

The Code
--------

<%@ Language=VBScript %>
<%

Dim objFSO

Dim objFolder
Dim objItem
Dim strCount
Dim strSize
Dim strTotalCount
Dim strTotalSize
Dim strPath
Dim strFolderCount

strCount = 0
strSize = 0
strTotalCount = 0
strTotalSize = 0
strFolderCount = 1

strPath="\"

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))

response.write "<TABLE cellpadding='2'><tr><TD align='left' style='border-Bottom-style: solid'><font face='Tahoma' size='2'><strong>Folder Name</td></strong>" &_
"<TD align='right' style='border-Bottom-style: solid'><font face='Tahoma' size='2'><strong>Total Files</strong></TD><TD align='right' style='border-Bottom-style: solid'><font face='Tahoma' size='2'><strong>Folder Size</strong></TD></TR>"


'Loop to get all files in root directory
'---------------------------------------

For Each objItem In objFolder.Files

strCount=strCount+1
strSize=strsize + round(objItem.Size/1024,2)

Next
Response.write ("<TR><TD><b><p><font face='Tahoma' size='2'>" & objFolder.Path & "</b></TD>" &_
"<TD align='right'><font face='Tahoma' size='2'>" & strCount & " file(s)" & "</TD>" &_
"<TD align='right'><font face='Tahoma' size='2'>" & strSize & " KB" & "</TD></TR>")

strTotalCount=strCount
strTotalSize=strSize
strSize=0
strCount=0

'Loop to get all files in subfolders
'---------------------------------------
For Each objFolder in objFolder.SubFolders

For Each objItem in objFolder.Files

strCount=strCount+1
strSize=strsize + round(objItem.Size/1024,2)

Next

Response.write ("<TR><TD><p><b><p><font face='Tahoma' size='2'>" & objFolder.Path & "</b></TD>" &_
"<TD align='right'><font face='Tahoma' size='2'>" & strCount & " file(s)" & "</TD>" &_
"<TD align='right'><font face='Tahoma' size='2'>" & strSize & " KB" & "</TD></TR>")

strTotalCount=strTotalCount+strCount
strTotalSize=strTotalSize+strSize
strSize=0
strCount=0
strFolderCount=strFolderCount+1
Next

Response.write ("<TR><TD style='border-Top-style: solid'><p><b><p><font face='Tahoma' size='2'>Total " & strFolderCount & " folder(s) </TD>" &_
"<TD align='right' style='border-Top-style: solid'><font face='Tahoma' size='2'>" & strTotalCount & " file(s)" & "</TD>" &_
"<TD align='right' style='border-Top-style: solid'><font face='Tahoma' size='2'>" & strTotalSize & " KB" & "</TD></TR></Table>")




Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

%>

-------------------------------------------------------

Any suggestions?


Cheers!
ÙÇãá

It's important to learn the rules so that you know how to break them.
 
you could use this code structure to scan through all directories and sub folders...
Code:
'strInput is the top level directory that you want to scan from
strInput = "C:\"

Set objFSO = CreateObject("Scripting.FileSystemObject")

myFunction(strInput)

Set objFSO = Nothing

'=====================================================
Function myFunction(strFolder)
  Set objFolder = objFSO.GetFolder(strFolder)

  For Each strFile In objFolder.Files
'CODE HERE TO PERFORM ON EACH FOLDER
  Next

  For Each strSubFolder In objFolder.SubFolders
    myFunction(strSubFolder.Path)
  Next
End Function

Tony
________________________________________________________________________________
 
Dear Tony

You put me on right track. Thanks for such a helpful tip.


Cheers!
ÙÇãá

It's important to learn the rules so that you know how to break them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top