Sunday, April 25, 2010

Write a script in VB to display only the Text Files in a Folder using FileSystemObject

VB Script used to display only the Text files of a Folder by using the GetFolder and Files method and also arrays to print all the Text Files.


'Step 1: Create and object variable to interact with the File system object
Set fso = CreateObject("Scripting.FileSystemObject")

'Step 2: Point to the folder from which we want to print
'the file names
Set fol = fso.GetFolder("F:\TestFolder")

'Step 3: Return all the files collection from the specified folder
Set fil = Fol.Files

'Print only the text file names from the specified folder
For each f in fil
str = f.name
arr = split(str, ".")
If arr(1) = "txt" then
var = var&Vbnewline&f.name
End If
Next

msgbox "The Text Files in the Folder "TestFolder" are: "&vbnewline&var

No comments:

Post a Comment