Results 1 to 5 of 5

Thread: Count Files in Folder & Subfolders, have searched to no avail!

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    18

    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:
    1. LogStream.WriteLine(LogStamp & "------Lab" & pathVar & " Begin Document Folder Cleaning")
    2.         mydocsSubFolderCount = 0
    3.         For Each mydocsSubFolder in mydocsFolder.SubFolders
    4.             If DateDiff("h", mydocsSubFolder.DateLastModified, now) > 24 Then
    5.                 mydocsSubFolderFiles = mydocsSubFolderFiles + mydocsSubFolder.Files.Count
    6.                 LogStream.WriteLine(LogStamp & mydocsFolder.Files.Count)
    7.                 mydocsSubFolder.Delete
    8.                 mydocsSubFolderCount = mydocsSubFolderCount + 1
    9.             End If
    10.         Next
    11.         LogStream.WriteLine(LogStamp & "------Lab" & pathVar & " " & mydocsSubFolderCount & " Document Folders Deleted")
    12.         LogStream.WriteLine(LogStamp & mydocsSubFolderFiles)
    13.         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.
    SP:.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    18
    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:
    1. Sub Main   
    2. mydocsSubFolderCount = 0
    3.         For Each mydocsSubFolder in mydocsFolder.SubFolders
    4.             Call Count(mydocsSubFolder)
    5.                 LogStream.WriteLine(LogStamp & mydocsSubFolderFiles)
    6.         Next
    7. 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:
    1. Sub Count(ByRef mydocsSubFolder)
    2.     cCount = mydocsSubFolder.Files.Count
    3.     mydocsSubFolderFiles = mydocsSubFolderFiles + cCount
    4.     cCount = 0
    5.     For Each mydocsSubFolder in mydocsSubFolder.SubFolders
    6.         Call Count(mydocsSubFolder)
    7.     Next
    8.    
    9. End Sub
    SP:.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    18
    /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!
    SP:.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    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:
    1. ' VBScript
    2. Option Explicit
    3.  
    4. Dim fs ' variable declared outside a procedure (this is a global variable)
    5.        ' this is hold a reference to the file system object
    6.  
    7. ' create an instance
    8. Set fs = CreateObject("scripting.filesystemobject")
    9.  
    10. ' count files in windows directory
    11. MsgBox CountFiles ("M:\Windows")
    12.  
    13. ' takes a string argument containing the name of the directory
    14. ' returns an integer contiang the nubmer of files in that direcrectory
    15. ' and all sub directories
    16. Function CountFiles (ByVal StrFolder)
    17.     Dim ParentFld
    18.     Dim SubFld
    19.     Dim IntCount
    20.  
    21.         ' note the use of the fs global variable
    22.     Set ParentFld = fs.GetFolder (StrFolder)
    23.    
    24.         ' count the number of files in the current directory
    25.     IntCount = ParentFld.Files.Count
    26.    
    27.     For Each SubFld In ParentFld.SubFolders
    28.         ' count all files in each subfolder - recursion point
    29.         IntCount = IntCount + CountFiles(SubFld.Path)
    30.     Next
    31.  
    32.     ' return counted files
    33.     CountFiles = IntCount
    34. End Function
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width