-
Apr 29th, 2004, 03:05 PM
#1
Thread Starter
Junior Member
Count Files in Folder & Subfolders, have searched to no avail!
Hi!
I'm trying to find an answer to a question that I know has been answered many times over, but for all I can see all of the answers pertain to VBCode and not VBScripting. I would like to be able to have something that would return the number of files in a folder and all of its subfolders as an Integer. I have seen solutions to this in VB using list boxes and Dir() Etc, but none of these apply to a simple script that has no interface. Can anyone give me any ideas ? :/
I am doing a loop that goes through a folder and deletes all of the files, and counts them, then a second loop after that that goes through a folder and deletes any subdirectories that are there but I have no way of counting those files, because I cannot set the actual pathname of the subdirectory.
Here is my code so far (Second Loop):
VB Code:
LogStream.WriteLine(LogStamp & "------Lab" & pathVar & " Begin Document Folder Cleaning")
mydocsSubFolderCount = 0
For Each mydocsSubFolder in mydocsFolder.SubFolders
If DateDiff("h", mydocsSubFolder.DateLastModified, now) > 24 Then
mydocsSubFolderFiles = mydocsSubFolderFiles + mydocsSubFolder.Files.Count
LogStream.WriteLine(LogStamp & mydocsFolder.Files.Count)
mydocsSubFolder.Delete
mydocsSubFolderCount = mydocsSubFolderCount + 1
End If
Next
LogStream.WriteLine(LogStamp & "------Lab" & pathVar & " " & mydocsSubFolderCount & " Document Folders Deleted")
LogStream.WriteLine(LogStamp & mydocsSubFolderFiles)
LogStream.WriteLine(LogStamp & "*")
My problem is that the .SubFolders object doesnt support the .Files.Count method.
Please help! Any sort of outside function would work if needed, I just need to get a number as an Integer.
-
Apr 29th, 2004, 04:22 PM
#2
My problem is that the .SubFolders object doesnt support the .Files.Count method.
This is becuase the subfolders object is collection of folders. You can have:
SubFolders.Count
to return the number of subfolders in the subfolders object and you can count the number of files in an individual subfolder:
SubFolders(index).Files.Count
The only object that supports the Files collection object is the Folder object.
-
Apr 29th, 2004, 04:48 PM
#3
Thread Starter
Junior Member
I did some more research and split my script into a Sub Main and a Sub Count for this counting files issue. This seems to run fine, however I cannot get the variables to move from sub to sub.
I edited out all of the parts that dont pertain to this for testing, so its only the counting that I am working with.
VB Code:
Sub Main
mydocsSubFolderCount = 0
For Each mydocsSubFolder in mydocsFolder.SubFolders
Call Count(mydocsSubFolder)
LogStream.WriteLine(LogStamp & mydocsSubFolderFiles)
Next
End Sub
This is the loop that goes through the subfolders of the parent directory, calling my count sub, and when the count sub ends it returns and is supposed to send the number to my log file, but it only returns a blank. I am assuming this is because the variable is cleared when the sub ends, and since this is VBScript and not a full program, there is no place for me to define a public variable?
VB Code:
Sub Count(ByRef mydocsSubFolder)
cCount = mydocsSubFolder.Files.Count
mydocsSubFolderFiles = mydocsSubFolderFiles + cCount
cCount = 0
For Each mydocsSubFolder in mydocsSubFolder.SubFolders
Call Count(mydocsSubFolder)
Next
End Sub
-
Apr 29th, 2004, 05:13 PM
#4
Thread Starter
Junior Member
/bonk self
Read about Option Explicit and decided that it would be helpful to actually TRY to define all the variables first as publics.
Topic almost resolved if I get this working correctly!
-
Apr 29th, 2004, 05:18 PM
#5
A VBs Script is a script. All variables which are declared outside a procedure are global and thus available to the entire script.
It looks to me as if you are trying to count all files in all subfolders of a parent folder. This can be done simply by using recursion:
VB Code:
' VBScript
Option Explicit
Dim fs ' variable declared outside a procedure (this is a global variable)
' this is hold a reference to the file system object
' create an instance
Set fs = CreateObject("scripting.filesystemobject")
' count files in windows directory
MsgBox CountFiles ("M:\Windows")
' takes a string argument containing the name of the directory
' returns an integer contiang the nubmer of files in that direcrectory
' and all sub directories
Function CountFiles (ByVal StrFolder)
Dim ParentFld
Dim SubFld
Dim IntCount
' note the use of the fs global variable
Set ParentFld = fs.GetFolder (StrFolder)
' count the number of files in the current directory
IntCount = ParentFld.Files.Count
For Each SubFld In ParentFld.SubFolders
' count all files in each subfolder - recursion point
IntCount = IntCount + CountFiles(SubFld.Path)
Next
' return counted files
CountFiles = IntCount
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|