Processing files contained in a folder
Reference page: Service Functions – System (QTP, VBScript)
Question
The question was asked here.
I need to check number of text files in the folder but i dont know names
Answer
Checking what number of files is contained in a folder is simple. More challenging tasks would be processing files based on type or name patterns.
Implementation
1. Retrieve total number of files
Dim sParentFolder
sParentFolder = "C:\TEMP"
Set FSO = CreateObject("Scripting.FileSystemObject")
boolRC = FSO.FolderExists(sParentFolder)
If Not boolRC Then
Set FSO = Nothing 'release an object
'do what you need to do if
'folder does not exist
End If
Set objParentFolder = FSO.GetFolder(sParentFolder)
Set objFilesColl = objParentFolder.Files
'objFilesColl.Count contains total number of files
2. Count files based on extension
Used resources: Service Functions – String (QTP, VBScript)
In the second example number of text files is just counted. Excel files’ names are stored in Dictionary object for further processing.
Dim sFolder
Dim FSO, objFolder, objFile, objXLSList
Dim intTXTCount
sFolder = "C:\TEMP"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objXLSList = CreateObject("Scripting.Dictionary")
Set objFolder = FSO.GetFolder(sFolder)
intTXTCount = 0
For Each objFile In objFolder.Files
If Regex_Test(objFile.Name, ".*\.[t,T][t,T][t,T]") Then
intTXTCount = intTXTCount + 1
End If
If Regex_Test(objFile.Name, ".*\.[x,X][l,L][s,S]") Then
objXLSList.Add objXLSList.Count, objFile.Name
End If
Next


One response to "Processing files contained in a folder"
Sir please give the function Regex_Test which is used in the code.
[ Albert’s reply: follow the link given in Used Resources section ]