WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

Folder search using FileSystemObject

Summary: Using the FileSystemObject (FSO), you can search folders for a particular filename.
 
Keywords: DIR
DIRECTORY
FILE
FILESYSTEMOBJECT
FOLDER
FSO
SEARCH
SUB
SUBFOLDER
SUB-FOLDER
Applicable Software: Active Server Pages (ASP)
 
Body:

Many times, a simple task of getting a list of files in a folder can become more difficult as you think about it.

Suppose the folder has subfolders. First thought is to just add a loop to go through the subfolders. Sounds good. But! What if those subfolders have subfolders? Now, what? Another loop? And, what if they go even deeper than that? And again? And again?

The answer is Recursion. A simple concept that can leave you baffled for days. Recursion is simply when a Function calls itself.

Here's how I chose to solve the task of searching folders and any subfolders that they have:

Dim contentPath, searchText
Dim totalFileCount, FSO, startDir
Dim matchedFileCount, matchedFileArray
Dim colCount, rowCount

	contentPath = "C:\iPlanet\Servers\docs\download\audio\"
	searchText = "omi"

	Set FSO = Server.CreateObject("Scripting.FileSystemObject")
	Set startDir = FSO.GetFolder(contentPath)
	Set FSO = Nothing


'** just get a count of all of the files:
	'totalFileCount = CountFiles( startDir )
	'Response.Write "Total Files Searched: " & totalFileCount & "<br />" & vbCrLf


'** get a count of the matched files:
	'totalFileCount = 0
	'matchedFileCount = CountMatchingFiles(searchText, startDir, totalFileCount)
	'
	'Response.Write "Total Files Searched: " & totalFileCount & "<br />" & vbCrLf & _
	'		"MatchedFileCount: " & matchedFileCount & "<br />" & vbCrLf


'** get an array of the matched files
	totalFileCount = 0
	matchedFileCount = 0
	Call GetMatchingFiles(searchText, startDir, totalFileCount, matchedFileCount, matchedFileArray)

	'Response.Write "Total Files Searched: " & totalFileCount & "<br />" & vbCrLf & _
			"MatchedFileCount: " & matchedFileCount & "<br />" & vbCrLf

	If IsArray(matchedFileArray) And matchedFileCount >= 1 Then
		ReDim Preserve matchedFileArray(UBound(matchedFileArray, 1), matchedFileCount - 1)

		For rowCount = 0 To UBound(matchedFileArray, 2)
			For colCount = 0 To UBound(matchedFileArray, 1)
				Response.Write colCount & "/" & rowCount & ": " & matchedFileArray(colCount, rowCount)
			Next
		Next
	End If

'******************* FUNCTIONS:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Function:
'	CountFiles
'Arguments:
'	parentFolder: Folder object which contains the SubFolders to process
'Returns:
'	Number of Files in all folders and sub-folders (recursively processed)
'Purpose:
'	Count the number of files in all folders and sub-folders.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CountFiles(parentFolder)
'Declare local variables
Dim fileCount

	'Set the fileCount to the # of files in the current folder
	fileCount = parentFolder.Files.Count

	'Loop through the sub-folders of the current folder
	For Each fldr In parentFolder.SubFolders
		'Debug message, display current sub-folder name
		'Response.Write fldr.Name & "<br />" & vbCrLf

		'Loop through the current sub-folder's files
		For Each file In fldr.Files
			'Debug message, display current file's information
			'Response.Write "File.Name = *" & File.Name & "*<br />" & vbCrLf
		Next

		'Recursively call this function to process sub-folders of the current sub-folder
		fileCount = fileCount + CountFiles( fldr )
	Next

	'Return the file count
	CountFiles = fileCount

End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Function:
'	CountMatchingFiles
'Arguments:
'	searchText: String that should be compared against the file's names
'	parentFolder: Folder Object which contains the SubFolders to process
'	totalFilesSearched: Counter of the total # of files
'Returns:
'	Number of files that contained the searchText in all folders and subfolders 
'	(recursively processed)
'Purpose:
'	Count the number of files whose filename contains a particular string.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CountMatchingFiles(ByVal searchText, ByVal parentFolder, ByRef totalFilesSearched)
'Declare local variables
Dim matchFileCount

	'Set the totalFilesSearched to the # of files in the current folder
	totalFilesSearched = totalFilesSearched + parentFolder.Files.Count
	
	'Loop through the sub-folders of the current folder
	For Each fldr In parentFolder.SubFolders
		'Debug message, display current sub-folder name
		'Response.Write fldr.Name & "<br />" & vbCrLf

		'Loop through the current sub-folder's files
		For Each file In fldr.Files
			'If the File's name contains the searchText
			If InStr(File.Name, searchText) > 0 Then
				'Then, increment a counter
				matchFileCount = matchFileCount + 1

			End If

		Next

		'Recursively call this function to process sub-folders of the current sub-folder
		matchFileCount = matchFileCount + CountMatchingFiles(searchText, fldr, totalFilesSearched)
	Next

	'Return the matching file count
	CountMatchingFiles = matchFileCount

End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Function:
'	GetMatchingFiles
'Arguments:
'	searchText: String that should be compared against the file's names
'	parentFolder: Folder Object which contains the SubFolders to process
'	totalFilesSearched: Counter of the total # of files
'	matchFileCount: Counter of the # of files that matched
'	matchFileArray: Array of information about files that matched
'Returns:
'	Nothing
'Purpose:
'	Get an array that contains information about the files whose filename contains a
'	particular string.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Declare constants for our array
Const MATCHFILE_FILENAME = 0
Const MATCHFILE_INDEXUSED = 1

'Declare constants about how our function works with the array
Const MATCHFILE_COLUMNS = 1
Const MATCHFILE_STEPSIZE = 10

Sub GetMatchingFiles(ByVal searchText, ByVal parentFolder, ByRef totalFilesSearched, ByRef matchFileCount, ByRef matchFileArray)

	'Set the totalFilesSearched to the # of files in the current folder
	totalFilesSearched = totalFilesSearched + parentFolder.Files.Count
	
	'Loop through the sub-folders of the current folder
	For Each fldr In parentFolder.SubFolders
		'Debug message, display current sub-folder name
		'Response.Write fldr.Name & "<br />" & vbCrLf

		'Loop through the current sub-folder's files
		For Each file In fldr.Files
			'If the File's name contains the searchText
			If InStr(File.Name, searchText) > 0 Then
				'Then, we will add it to the array

				'If the array hasn't been created yet
				If Not IsArray(matchFileArray) Then
					'Then, create it.
					ReDim matchFileArray(MATCHFILE_COLUMNS, MATCHFILE_STEPSIZE)

				ElseIf matchFileArray(MATCHFILE_INDEXUSED, UBound(matchFileArray, 2)) = "used" Then
					'Otherwise, if the last record in the array is used, we want to make it bigger
					ReDim Preserve matchFileArray(MATCHFILE_COLUMNS, UBound(matchFileArray, 2) + MATCHFILE_STEPSIZE)

				End If

				'Put the current file's information into the array
				'For now, we are just storing the name of the file
				matchFileArray(MATCHFILE_FILENAME, matchFileCount) = File.Name
				matchFileArray(MATCHFILE_INDEXUSED, matchFileCount) = "used"

				'Increment our matched file counter
				matchFileCount = matchFileCount + 1
			End If

		Next

		'Recursively call this function to process sub-folders of the current sub-folder
		Call GetMatchingFiles(searchText, fldr, totalFilesSearched, matchFileCount, matchFileArray)
	Next

End Sub

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 10/29/2002 10:32:46 AM

Rate this article: Average: n/a
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.