VBScript - Getting the Contents of a Folder
This section explains how to get the contents of a particular folder. The content of a folder
can be two types. Files or Sub Folders. The File System Object provides a function named 'GetFolder'
to get a list of files and sub folders. The syntax of the function is given below.
Syntax: GetFolder(FolderPath)
Here the argument 'FolderPath' is a mandatory argument. It specifies the complete path to
the folder.
The following code shows how to get a list of files from a particular folder. This code will display
hidden files also.
Getting the List of Files in a Folder
Dim ObjFso
Dim StrFolderName
Dim ObjFolder
Dim ObjFilesCollection
Dim ObjFile
On Error Resume Next
StrFolderName = "C:\TestFolder"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Getting the folder
Set ObjFolder=ObjFso.GetFolder(StrFolderName)
'Getting the list of files in to a collection object
Set ObjFilesCollection=ObjFolder.Files
'Printing each file in the collection object
For Each ObjFile In ObjFilesCollection
'Printing the file name
WScript.Echo(ObjFile.Name)
'Printing the file name with path
WScript.Echo(ObjFile.Path)
Next
If Err.Number <> 0 Then
WScript.Echo("An error occured.")
End If
The following code shows how to get a list of subfolders of a folder including hidden folders.
This code will not display the sub folders of sub folder.
Getting the List of Sub Folders In a Folder
Dim ObjFso
Dim StrFolderName
Dim ObjFolder
Dim ObjSubFolderCollection
Dim ObjSubFolder
On Error Resume Next
StrFolderName = "C:\TestFolder"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Getting the folder
Set ObjFolder=ObjFso.GetFolder(StrFolderName)
'Getting the list of sub folders in to a collection object
Set ObjSubFolderCollection=ObjFolder.SubFolders
'Printing each sub folder in the collection object
For Each ObjSubFolder In ObjSubFolderCollection
'Printing the sub folder name
WScript.Echo(ObjSubFolder.Name)
'Printing the sub folder name with path
WScript.Echo(ObjSubFolder.Path)
Next
If Err.Number <> 0 Then
WScript.Echo("An error occured.")
End If
Now obviously your next question will be 'How can I get all the files and sub folders that are
any level deep?' There is no direct function available for this. But by making use of a recursive
function we can achieve this. The following code demonstrates this.
Getting All Files and All Sub Folders of a Folder (At Any Depth)
Dim ObjFso
Dim StrFolderName
On Error Resume Next
StrFolderName = "C:\Test Folder"
'Creating a file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Calling this function to list all files and sub folders
Call ListSubFoldersAndFiles(StrFolderName)
'This function will recursively call itself to display
'all files and subfolders
Function ListSubFoldersAndFiles(StrSubFolderPath)
Dim ObjFolder
Dim ObjFileCollection
Dim ObjSubFolderCollection
Dim ObjSubFolder
Dim ObjFile
'Getting the folder
Set ObjFolder=ObjFso.GetFolder(StrSubFolderPath)
'Getting the list of files
Set ObjFileCollection=ObjFolder.Files
'Printing each file
For Each ObjFile In ObjFileCollection
WScript.Echo(ObjFile.Path)
Next
'Getting the list of sub folders
Set ObjSubFolderCollection=ObjFolder.SubFolders
For Each ObjSubFolder In ObjSubFolderCollection
'Printing each sub folder
WScript.Echo(ObjSubFolder.Path)
'Calling this function recursively to list files and
'sub folders from this sub folder
Call ListSubFoldersAndFiles(ObjSubFolder.Path)
Next
End Function