VBS Script to report the properties of all files in a folder and its sub folders

I recently had a need to list the  properties of all files in a folder and its sub folders to find files that had not been used in a long time, or files where the owner had left. In my case I needed to run a report on approx 250,000 files which this VBS script managed.

The VBS script will get the following properties;

  • The file path and name
  • The file size file type
  • Date Created
  • Date Last Accessed
  • Date Last Modified
  • File Owner (not available for all files)

To use the VBS script download the attached file or copy the below script to a text file and save as something.vbs, run the script by double clicking on it, and browse to a folder you want to report on.

Depending on the number of files the VBS script may take a while to run, when it is complete a CSV file will be generated in the same folder as the script which can then be imported into something like Excel and you can do some nice reports. Remember Excel 2003 can only handle a maximum of 65,536 rows so if you have scanned more files than that then Excel 2007 will do the job.

on error resume next

' Flags for browse dialog
Const BIF_returnonlyfsdirs   = &H0001
Const BIF_dontgobelowdomain  = &H0002
Const BIF_statustext         = &H0004
Const BIF_returnfsancestors  = &H0008
Const BIF_editbox            = &H0010
Const BIF_validate           = &H0020
Const BIF_browseforcomputer  = &H1000
Const BIF_browseforprinter   = &H2000
Const BIF_browseincludefiles = &H4000

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDlg = WScript.CreateObject("Shell.Application")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

'Get the Source Folder
' Use the BrowseForFolder method.
Set objStartFolder = objDlg.BrowseForFolder (&H0, _
    "Please select the FOLDER to report on.", BIF_editbox + BIF_returnonlyfsdirs)

' Here we use TypeName to detect the result.
If InStr(1, TypeName(objStartFolder), "Folder") > 0 Then
        sourceFolder = objStartFolder.ParentFolder.ParseName(objStartFolder.Title).Path
Else
        MsgBox "An error has occured: Unable to read destination folder"
End if

'Ask to open the report now or just close
strMbox = MsgBox("Are youn sure you want to run the report of: " & sourceFolder & chr(13) & chr(10) & chr(13) & chr(10) & "If you continue this may take an exteneded period of time, a message will be displayed when complete, continue?",4,"Are you sure?")
       
if strMbox = 6 Then
        currentScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
        reportFile = currentScriptPath & "File_Properties_Report.csv"
       
        'OpenTextFile(destination, forwriting, createnew, open as Unicode)
        Set objReportFile = objFSO.OpenTextFile(reportFile, ForWriting, True, True)
       
        'Add headers
        objReportFile.Write("Path, Size(kb), Type, Created, Last Accessed, Last Modified, Suggested Action, Owner"  & chr(13) & chr(10))
       
        'Run though file report process
        ReportFiles sourceFolder
       
        'Close the file
        objReportFile.Close
       
        'Compete
        strMbox = MsgBox("Report Complete")
End if

Function ReportFiles(currentFolder)
   Dim objFolder, objFile, fileCollection, folderCollection, subFolder

   Set objFolder = objFSO.GetFolder(currentFolder)
   Set fileCollection = objFolder.Files
   
   For Each objFile In fileCollection
   
                'Get File Properties
                strFilePath = objFile.Path
                strFileName = objFile.Name
                strFileSize = objFile.Size / 1024
                strFileType = objFile.Type
                strFileDateCreated = objFile.DateCreated
                strFileDateLastAccessed = objFile.DateLastAccessed
                strFileDateLastModified = objFile.DateLastModified

                'Get File owner
                strFileOwnerDomain = ""
                strFileOwner = ""

                strComputer = "."
                        Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
               
                if strFileType <> "Shortcut" or InStr(1,strFileName, "AlbumArt",1) = 0 or InStr(1,strFileName, "£",1) Then
                        Set colItems = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=""" & Replace(strFilePath, "\", "\\") & """}" & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
                       
                        For Each objItem in colItems
                                strFileOwnerDomain =  objItem.ReferencedDomainName
                                strFileOwner = objItem.AccountName
                        Next
                End If
               
                objReportFile.Write(chr(34) & strFilePath & chr(34) & ", " _
                                                        &  Round(strFileSize,2) & ", " _
                                                        & chr(34) & strFileType & chr(34) & "," _
                                                        & strFileDateCreated & "," _
                                                        & strFileDateLastAccessed & "," _
                                                        & strFileDateLastModified & "," _
                                                        & chr(34) & strFileOwnerDomain & "\" & strFileOwner & chr(34) & "," _
                                                        & chr(13) & chr(10))   
        Next

        'Loop for each sub folder
   Set folderCollection = objFolder.SubFolders
       
    For Each subFolder In folderCollection
       ReportFiles subFolder.Path
   Next
End Function

11 thoughts on “VBS Script to report the properties of all files in a folder and its sub folders”

  1. Excelent script… but I suggest change the comma separator for a pipe “|” because there are countries (like Colombia) that use the comma “,” in decimal formats and not point “.”.

    Then when you separete the CSV in columns you can found mistakes with the file size.

    Another thing… how can I do to scan a dir path in a server like \\servidor1\shared?

    Reply
  2. Very nice script, I found it when searching on Google. I note that the script ignores any files/folders which have the hidden and.or system attribute set. Could it be modified to work on any file/folder (I do not have the knowledge to do such a modification) ?

    Reply
  3. love this but when i do a test run on my local computer. it drops everything in one column in excel?
    is there a way to seperate each field into its own column ?

    thanks… new to scripting.

    Reply
    • sorry please disregard my last post…

      BUT … when i run it on a server share… it stops at 787 files…. anyway to make it look at all files ??

      Reply
      • Hi,

        I am glad you sorted the Excel issue. It’s been a few years since I wrote this script, I believe I last tested it on Windows 7 / Server 2008 and at the time it worked against several thousand files.

        Let me do some testing and see if I can spot anything obvious, do you get any error messages? If so could you provide a screenshot?

        If the script is stopping at a certain file it may be worth making sure you have read access to the file etc.

        Thanks
        Phil

        Reply
        • no errors. says complete.
          i ran it on the server with admin rights and it got much farther but still didnt complete.

          thanks
          Peter

          Reply
        • Hi Phil, I have a requirement to display the total size and used space of source directory and subfolder sizes of sources directory , and then once we pass the required directory your current script will give us the required details.can this be achieved ?

          Reply
  4. Super Script and it gives awesome results, if possible , can u add the size of the source folder and size of the sub folders and size in it , and at the end we can provide our destination path and the current script gives all the details. , can this be achieved, please email me

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.