List Files in a Folder Using VBScript

0

If you need to list files from a folder you can use the following VBScript. It outputs file properties of all files within a specified folder to a text file. This can be modified to create XML or CSV outputs.

You can choose which file attributes to include in the output.

Here is the script. Copy and paste it into a text file but give it a VBS file extension. Place it in the same location as the folder you wish to scan.

Dim fso, folder, files, OutputFile
Dim strPath

' Create a FileSystemObject  
Set fso = CreateObject("Scripting.FileSystemObject")

' Define folder we want to list files from
strPath = "mySubDirectory"

Set folder = fso.GetFolder(strPath)
Set files = folder.Files

' Create text file to output test data
Set OutputFile = fso.CreateTextFile("ScriptOutput.txt", True)

' Loop through each file  
For each item In files

  ' Output file properties to a text file
  OutputFile.WriteLine(item.Name)
  OutputFile.WriteLine(item.Attributes)
  OutputFile.WriteLine(item.DateCreated)
  OutputFile.WriteLine(item.DateLastAccessed)
  OutputFile.WriteLine(item.DateLastModified)
  OutputFile.WriteLine(item.Drive)
  OutputFile.WriteLine(item.Name)
  OutputFile.WriteLine(item.ParentFolder)
  OutputFile.WriteLine(item.Path)
  OutputFile.WriteLine(item.ShortName)
  OutputFile.WriteLine(item.ShortPath)
  OutputFile.WriteLine(item.Size)
  OutputFile.WriteLine(item.Type)   
  OutputFile.WriteLine("")
  
Next

' Close text file
OutputFile.Close

Make sure you replace “mySubDirectory” with the name of the folder you wish to list files from.

The script follows these steps :

  • Dimension FileSystemObject variables
  • Dimension other variables
  • Create FileSystemObject
  • Define the path
  • Get file collection
  • Loop through collection of files
  • Output attributes to text file

You can exclude a property from the output file by removing the appropriate line above or commenting it out by placing a single quote character at the beginning of the line.

List File Properties to CSV File

In this modified version selected properties are comma separated. When loaded into a spreadsheet this would give you 1 file per line with each property in a separate column.

Dim fso, folder, files, OutputFile
Dim strPath

' Create a FileSystemObject  
Set fso = CreateObject("Scripting.FileSystemObject")

' Define folder we want to list files from
strPath = "mySubDirectory"

Set folder = fso.GetFolder(strPath)
Set files = folder.Files

' Create CSV file to output test data
Set OutputFile = fso.CreateTextFile("ScriptOutput.csv", True)

' Loop through each file  
For each item In files

  ' Output file properties to a text file
  OutputFile.Write(item.Name)
  OutputFile.Write(",")
  OutputFile.Write(item.DateCreated)
  OutputFile.Write(",")
  OutputFile.Write(item.DateLastAccessed)
  OutputFile.Write(",")
  OutputFile.Write(item.DateLastModified)
  OutputFile.Write(",")
  OutputFile.WriteLine(item.Size)
  
Next

' Close text file
OutputFile.Close

The script follows these steps :

  • Dimension FileSystemObject variables
  • Dimension other variables
  • Create FileSystemObject
  • Define the path
  • Get file collection
  • Loop through collection of files
  • Output attributes to text file separating with a comma

File Objects Properties

The following is a list of properties available for File objects :

Attributes Property
This property allows us to get or change the various attributes of a file.
Syntax: object.Atributes [ = newattributes]

DateCreated Property
This property gets the date and time that the file was created.
Syntax: object.DateCreated

DateLastAccessed Property
Gets the date and time that the file was last accessed.
Syntax: object.DateLastAccessed

DateLastModified Property
This property returns the date and time that the file was last modified.
Syntax: object.DateLastModified

Drive Property
Returns the drive letter of the drive where the file is located.
Syntax: object.Drive

Name Property
Lets us get or change the name of the specified file.
Syntax: object.Name [ = newname]

ParentFolder Property
This property gets the Folder object for the parent relating to the specified file.
Syntax: object.ParentFolder

Path Property
This property returns a file’s path.
Syntax: object.Path

ShortName Property
Returns the short version of a filename (using the 8.3 convention).
e.g. Employees.html is truncated to Employ~1.htm
Syntax: object.ShortName

ShortPath Property
Returns the short version of the file path (this is the path with any folder and file names truncated as above).
Syntax: object.ShortPath

Size Property
Returns the size of a file in bytes.
Syntax: object.Size

TypeProperty
Returns a string containing the file type description.
e.g. For files ending in .TXT, “Text Document” is returned.
Syntax: object.Type

Text Stream Object Reference

For information on text stream object that is used in these scripts please take a look at the Office VBA Reference.

Share.

Leave A Reply

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