search folder and subfolder for filename

I don’t know if I’m just searching for the wrong thing or what but I know it’s been covered on here before but I can’t seem to find my answer…

How do I search a specified folder and it’s subfolders for a file named “Info.txt”?

Thank you

Hi,

as the Finder or System Events are quite slow while searching in entire contents of,
the shell commands find or mdfind (using Spotlight) are more effective

Thanks for the tip. Unbearably slow but it works…

			tell application "Finder" to set theFiles to every file in the entire contents of folder "Macintosh HD:Users:admin:Documents:"
			
			repeat with aFile in theFiles
				set fileName to aFile's name
				set theFile to aFile as string
				
				if fileName is equal to "Info.txt" then
					if (paragraph 1 of (read alias theFile)) = "Line 1" then
						display dialog "Yep, line 1."
					else
						display dialog "Nope, not line 1."
					end if
				else
					--
				end if
			end repeat

Browser: Safari 531.9
Operating System: Mac OS X (10.6)

this is probably much faster


set documentsPOSIXpath to POSIX path of (path to documents folder)

do shell script "/usr/bin/mdfind -onlyin " & quoted form of documentsPOSIXpath & " 'kMDItemFSName = \"info.txt\"'"
repeat with aFile in (get paragraphs of result)
	if paragraph 1 of (read (POSIX file aFile as alias)) = "Line 1" then
		display dialog "Yep, line 1."
	else
		display dialog "Nope, not line 1."
	end if
	
end repeat

Holy crap! You weren’t kidding around with the shell script speed! That is almost immediate results compared to about a 5-10 second delay with mine!

Thanks for the tip! I have some other stuff that may benefit from kMDItemFSName so I’m going to go do some reading. Thanks again!

Browser: Safari 531.9
Operating System: Mac OS X (10.6)

To find out what other parameters are searchable with mdfind, it’s useful to run this against a sample:

set tFile to (choose file)
set metaData to do shell script "mdls " & quoted form of POSIX path of tFile

Note that “and” is && to combine characteristics

FWIW, I have found on 10.5 that “getting every file” seems really slow (at least over file servers), but asking for just one attribute is faster. That is “get every item of folder X” takes forever, but “get name of every item of folder X” seems to be much faster.
Again this might just be our (puke) Windows servers.