How to retrieve folder contents with modification dates

How to retrieve folder contents with modification dates


Working with files on Windows can be enhanced with utilities to monitor or track their behavior. Depending on their use, scripts can reduce time spent on navigating through logs, determining their usage, archiving details on their changes, or reporting the history of folder contents.

Scripting on the Windows command line can be approached with various methods, but VBScript script files are incredibly capable. They are less limited than batch files in many regards and have similarities with more powerful languages like Visual Basic for Applications.

Working with folders and file contents can demonstrate some capabilities of VBScript files.




This example is meant to demonstrate some behavior of how a .vbs script can work with files.

A .vbs script can be stored in a text file, which uses a .vbs file extension.

In this example, a folder’s contents will be retrieved, stored in a single text string along with the file modification dates, and written to a file. A .csv file will be used for output, which can be opened with Excel, its data sorted, filtered, and even charted.

First, the .vbs file will need to declare variables for later use:

Dim fso, folder, files, file, folderContents, csv

This can be on the first line of the file, where we declare a FileSystem Object, variables for a folder and its contents, and a variable for a file with a .csv extension. This is where the output will go. The variables can be named however you like.

Then, we create a FileSystem Object, use the folder, and one of the file variables:

Set fSO = CreateObject(“Scripting.FileSystemObject”)
Set folder = fSO.GetFolder(“C:\PATH_TO_FOLDER\FOLDER”)
Set files = folder.Files

Alternatively, the folder can be replaced with a variable in the GetFolder function that contains the path as a string.

Then, we iterate through the folder, retrieving the contents:

For Each file in files
        folderContents = folderContents & & “,” & file.DateLastModified
        folderContents = folderContents & vbCrLf

This block is only four lines, but is more involved than the other parts of the script.

The for loop begins, iterating through each file in the total set of files. Each iteration will refer to a single file, using the file variable. Then, we start defining our output string, stored in the variable folderContents. We assign the string with the current file name using, then append a comma, and then the file’s last modification date. A .csv file separates each row’s values into separate columns using commas. Also, VBScript uses the & operator to append different values or strings to the end of a string.

On the next line, we add a newline, defined in VBScript as vbCrLf. Then, we fetch the next file in the set of files.

Then, we can create a file to store the results:

Set csv = fSO.CreateTextFile(“C:\PATH_TO_OUTPUT_FILE\OUTPUT_FILE_NAME.csv”)

Again, the output path can be passed as an argument to the CreateTextFile function using a variable.

Then, we write the output data to the output file, close the file, and for debugging purposes, can write the output data to the commandline:



When you run the script from the command line, the output file should be created, and will be readable with Excel. You can run the script using CScript with a similar command to:

cscript SCRIPT_FILE_NAME.vbs