Facebook Twitter Instagram
    Trending
    • Hard Drive Repair After Wrong Voltage Mishap
    • How To Send Excel Workbook As Email Using Button
    • Compare EV Chargers
    • Home Assistant Server Monitoring
    • Mitsubishi Outlander Dash Cam Install
    • Left or Right Pad a String with a VBScript Function
    • List Files in a Folder Using VBScript
    • How to Verify Checksum of a File
    RSS Twitter Instagram Facebook Pinterest YouTube
    Technology Spy
    • Tutorials & Help
    • Reviews
    Technology Spy
    You are at:Home»Tutorials & Help»List Files in a Folder Using VBScript
    Visual Basic Applications

    List Files in a Folder Using VBScript

    0
    By Matt on January 21, 2021 Tutorials & Help, VBScript

    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. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow to Verify Checksum of a File
    Next Article Left or Right Pad a String with a VBScript Function

    Related Posts

    Left or Right Pad a String with a VBScript Function

    How to Verify Checksum of a File

    Lenovo T460 Trackpad Ubuntu 18.04 Fixes

    Leave A Reply Cancel Reply

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

    Recent Posts
    November 20, 2022

    Hard Drive Repair After Wrong Voltage Mishap

    March 22, 2022

    How To Send Excel Workbook As Email Using Button

    January 30, 2022

    Compare EV Chargers

    Categories
    • 3D Printing
    • Android
    • Arduino
    • Development
    • ESP8266
    • Excel
    • Gaming
    • General
    • GIMP
    • Home Automation
    • JavaScript
    • Linux
    • Microsoft Office
    • Mobile Devices
    • Oracle APEX
    • Python
    • Raspberry Pi
    • Reviews
    • Security
    • SharePoint
    • Tutorials & Help
    • VBScript
    Tags
    Android APEX Apple Arduino Black Friday csv Elite Dangerous email ESP-01 ESP8266 EV Excel file handling gaming GIMP Gritin Home Assistant Home Automation IMNEED Joystick Linux lists Lumsing Media os.stat os.walk Phishing Power Python Qi Review scam Security select SharePoint 2010 string Syncwire text text files TrueCrypt Ubuntu USB-C VBA VBscript Windows 10
    Web Tools

    A set of quick and basic online tools for web designers and software developers :

    • Hash Generator
    • Text to HTML List
    • Text to HTML Table
    • URL Encoder and Decoder
    • UNIX Timestamp Calculator
    • LED Resistor Calculator
    • Extract Email from Text
    • Mortgage Calculator
    About

    Welcome to Technology Spy, a site devoted to gadgets, computers, programming and all things technology! You’ll also find product reviews for items I own as well as tutorials, guides and scripts for the software I use.

    Archives
    Other Resources
    • MattsBits
    • Raspberry Pi Spy
    Facebook Twitter Instagram Pinterest

    Entries RSS | Comments RSS

    Copyright © 2022 - All Rights Reserved - Matt Hawkins

    mastodon.social@MattHawkins

    Type above and press Enter to search. Press Esc to cancel.