Submitted By: Jason Clement

Retrieves a list of files using the same format and wildcards as the DIR command. Run the script in the same fashion you would the DIR command; for example, cscript dir.vbs c:\files\f*.txt /S

Visual Basic
Edit|Remove
Option Explicit

Dim objShell, objExec
Dim strCommand
Dim strFiles, strFile

strCommand = "%comspec% /C DIR " & WScript.Arguments(0) & " /B"

' Optional argument /S tells the DIR command to list files
' in the directory and its subdirectories.
If WScript.Arguments.Named.Exists("S") Then _
    strCommand = strCommand & " /S"

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCommand)

strFiles = Split(objExec.StdOut.ReadAll(), VbCrLf)

' Last Entry will be a blank line
ReDim Preserve strFiles(UBound(strFiles) - 1)

For Each strFile in strFiles
    ' Do Action
    WScript.Echo strFile
Next