Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Folder.Files Collection

Status
Not open for further replies.

sifitz

Programmer
Feb 27, 2002
47
GB
Hi,

I want to print out all files in a folder that suit certain criteria. I have a large amount of files and a number of criteria to meet.

The first criteria I have is a filename filter. In VB you can use the Dir Function with a filter string. e.g. *.gif for all GIF files. Is there a method like this within the FileSystemObject?

Thanks,

Si Fitz [afro]
 
You can use files collections this way (note that in this example, fs is the filesystem object that has been define as global var):
Code:
'************************************************************************************
Function FolderContainsFilesWithExt(folderName, extension)
'************************************************************************************
	Dim fol, fil
	Dim cnt

	FolderContainsFilesWithExt = false
	if not fs.FolderExists(folderName) Then
		Exit function
	End if
	Set fol = fs.getFolder(folderName)
	For each fil in fol.files
		if extension = "*" Then
			FolderContainsFilesWithExt = true
			exit for
		end if
		if(right(fil.name,len(extension)) = extension) Then
			FolderContainsFilesWithExt = true
			exit for
		end if
	Next

	Set fol = Nothing
	Set fil = Nothing
End Function
Water is not bad as long as it stays out human body ;-)
 
Thanks, but that was not what I was looking for. I have managed to perform the task I was after, but it probably isn't as efficient as if I was able to perform an immediate filter rather than test the full folder for matches in my code.

Si [afro]
 
You can use the FileSystemObject method "FileExists" but it doesn't allow joker chars like "*" Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top