How to Create a Simple File Listing Script with VBScript

In this tutorial, we will walk you through creating a simple VBScript that allows you to select a folder from any drive and list all files within that folder. This script is particularly useful for automating file management tasks on Windows.

Why Use VBScript?

VBScript is an excellent tool for automation on Windows due to its ease of use and the ability to interact directly with the operating system and applications.

Step-by-Step Guide:

1. Open Notepad

First, open Notepad or any text editor of your choice.

2. Copy and Paste the Script

Copy the following VBScript code into your text editor:


        '' Create a Shell object
Set objShell = CreateObject("Shell.Application")

' Open the folder selection dialog, starting at the root of the file system
Set objFolder = objShell.BrowseForFolder(0, "Select a folder:", &H0011)

' Check if a folder was selected
If Not objFolder Is Nothing Then
    Set objFolderItem = objFolder.Self
    folderPath = objFolderItem.Path
    
    ' Create a FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Get the selected folder
    Set folder = fso.GetFolder(folderPath)
    
    ' Initialize a variable to store the list of files
    fileList = "Files in " & folderPath & ":" & vbCrLf & vbCrLf
    
    ' Loop through each file in the folder
    For Each file In folder.Files
        fileList = fileList & file.Name & vbCrLf
    Next
    
    ' Display the list of files in a message box
    MsgBox fileList, vbInformation, "Files List"
Else
    ' Display a message if no folder was selected
    MsgBox "No folder selected.", vbExclamation, "Folder Selection"
End If
    
3. Save the Script

Save the file with a .vbs extension, for example, ListFilesInAnyFolder.vbs. Make sure to select "All Files" in the Save as type dropdown to avoid saving it as a .txt file.

4. Run the Script

Double-click the saved .vbs file to execute the script. A folder selection dialog will appear, allowing you to select a folder from any drive. After selecting a folder, a message box will display the list of files in that folder.

Comments

Popular posts from this blog

Displaying a Calendar in the Command Prompt Using Python

Building a Caps Lock and Num Lock Indicator with Python