Processing files contained in a folder

Posted by Albert Gareev on Mar 15, 2010 | Categories: File System OperationsHow to

Reference page: Service Functions – System (QTP, VBScript)


The question was asked here.

I need to check number of text files in the folder but i dont know names


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.


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

  • One response to "Processing files contained in a folder"

  • kumar
    9th November 2011 at 4:26

    Sir please give the function Regex_Test which is used in the code.

    [ Albert’s reply: follow the link given in Used Resources section ]

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.