List All Files and Subfolders Inside a Folder Using VBScript

This article explains how to list all the files and subfolders inside a folder using vb script. To get all the files inside a folder, we can use the "Files" property of the folder object. Similarly to get all the subfolders, we can use the "Subfolders" property of the folder object. 

Note: These properties (Files and Subfolders), will give you the list of files and folders inside a specific folder only. If you want to list all the files inside subfolders at any level deep, there is no bult-in function in vbscript. Refer the script at the bottom which recursively examine all subfolders and list all files at any level depth.

Script to List all Immediate Files Inside a Folder

Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile

'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Getting the Folder Object
Set ObjFolder = fso.GetFolder("C:\Windows")

'Creating an Output File to write the File Names
Set ObjOutFile = fso.CreateTextFile("C:\WindowsFiles.txt")

'Getting the list of Files
Set ObjFiles = ObjFolder.Files

'Writing Name and Path of each File to Output File
For Each ObjFile In ObjFiles
	ObjOutFile.WriteLine(ObjFile.Name & String(50 - Len(ObjFile.Name), " ") & ObjFile.Path)
Next

ObjOutFile.Close

Script to List all Immediate Folders Inside a Folder

Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjSubFolders
Dim ObjSubFolder

'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Getting the Folder Object
Set ObjFolder = fso.GetFolder("C:\Windows")

'Creating an Output File to write the Folder Names
Set ObjOutFile = fso.CreateTextFile("C:\WindowsFiles.txt")

'Getting the list of SubFolders
Set ObjSubFolders = ObjFolder.SubFolders

'Writing Name and Path of each Subfolder to Output File
For Each ObjSubFolder In ObjSubFolders
	ObjOutFile.WriteLine(ObjSubFolder.Name & String(50 - Len(ObjSubFolder.Name), " ") & ObjSubFolder.Path)
Next

ObjOutFile.Close

Recursively List All Files and SubFolders Inside a Folder at Any Level Depth

Dim fso
Dim ObjOutFile

'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Create an output file
Set ObjOutFile = fso.CreateTextFile("OutputFiles.csv")

'Writing CSV headers
ObjOutFile.WriteLine("Type,File Name,File Path")

'Call the GetFile function to get all files
GetFiles("C:\Windows\Help")

'Close the output file
ObjOutFile.Close

WScript.Echo("Completed")

Function GetFiles(FolderName)
	On Error Resume Next
	
	Dim ObjFolder
	Dim ObjSubFolders
	Dim ObjSubFolder
	Dim ObjFiles
	Dim ObjFile

	Set ObjFolder = fso.GetFolder(FolderName)
	Set ObjFiles = ObjFolder.Files
	
	'Write all files to output files
	For Each ObjFile In ObjFiles
		ObjOutFile.WriteLine("File," & ObjFile.Name & "," & ObjFile.Path)
	Next
	
	'Getting all subfolders
	Set ObjSubFolders = ObjFolder.SubFolders
	
	For Each ObjFolder In ObjSubFolders
		'Writing SubFolder Name and Path
		ObjOutFile.WriteLine("Folder," & ObjFolder.Name & "," & ObjFolder.Path)
		
		'Getting all Files from subfolder
		GetFiles(ObjFolder.Path)
	Next
	
End Function
Powered by Bullraider.com