• Count of folders and files (WinXP/XL 2003-UK)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Count of folders and files (WinXP/XL 2003-UK)

    Author
    Topic
    #460774

    Hi,

    When right clicking a folder – “Properties”, the General tab ao. displays “Contains” and then states counts of folders and files.

    How are those easily counted – would anyone willing to share, have a snippet that does this job fast and efficient?

    Been looking to use File System Object (FSO), but I believe there must be other ways that are easier and more effecient as well??

    Would imagine there was some kind of Win API function available for this kind of task…?

    Viewing 0 reply threads
    Author
    Replies
    • #1166443

      Working with FileSystemObject is quite easy:

      Dim fso As New Scripting.FileSystemObject
      Dim fld As Scripting.Folder
      Dim lngSubFolderCount As Long
      Dim lngFileCount As Long
      Set fld = fso.GetFolder(“C:MyFolder”)
      lngSubFolderCount = fld.SubFolders.Count
      lngFileCount = fld.Files.Count

      • #1166447

        Hi Hans,

        Thanks – the “scripting” references you got set – are they to the “Microsoft Scripting Runtime” (scrrun.dll)??

        Dim fso As New Scripting.FileSystemObject
        Dim fld As Scripting.Folder

        Got one main folder, with 350+ subfolders. Each subfolder have at least the single file I’m looking for (to update) directly in it. But some have had another subfolder added, so that I need to find the file to update in the subfolder instead.

        Thus, I’m trying to see, if I with the “LookIn” and “SearchFolders” (http://msdn.microsoft.com/en-us/library/aa210046(office.11).aspx) – or any other method, can find an easy way to get a tree structure to traverse.

        Subsequently I’ll need to write the changes to the files in a log file. With “change done”, “file name” and “folder”.

        • #1166448

          Scripting indeed refers to the Microsoft Scripting Runtime library – you need to set a reference to it in Tools | References…

          In Office 2003 VBA you can use the Application.FileSearch object to search for files in a folder structure, but keep in mind that this has been removed in Office 2007, so if you plan on upgrading anytime soon, it’s better to avoid using Application.FileSearch.

        • #1166556

          Take note Henrik, that the code as provided by Hans counts only the files and folders at the top level within the specified folder.

    Viewing 0 reply threads
    Reply To: Count of folders and files (WinXP/XL 2003-UK)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: