Search for a particular file/s in sub folders from the server - Help

Hi All,

I am trying to search for 2 files (say abc1 & abc2) from a server which contains many main folders. These 2 files reside in one of the subfolders within the main folder. I need a script to go thru all the folders looking for files “abc1” & “abc2” and generate a text file or csv file. This text file should show the availability of these 2 files.
Example:
/users/john/job1/ abc1 Available
/users/john/job1/ abc2 Available
/users/john/job2/ abc1 Available
/users/john/job2/ abc2 Not Available
/users/Tom/job1/ abc1 Not Available
/users/Tom/job1/ abc2 Not Available

So far, i could only get the script to search for the one file, i need some assistance to expand the script to generate the text file as mentioned below.


set deskopPath to quoted form of (POSIX path of (choose folder))
set spotlightquery to "\"kMDItemFSName == 'abc1\""
set command to "mdfind -onlyin " & deskopPath & " " & spotlightquery
set founditems to paragraphs of (do shell script command)

Thanks in advance

JaiM

Model: MacBook Pro
AppleScript: 2.2.1
Browser: Safari 530.19
Operating System: Mac OS X (10.5)

Why not just iterate? I don’t think mdfind will accept a list of files.

set soughtFiles to {"'ABC_1.jpg'", "'ABC_2.jpg'"}
set foundFiles to {}
set deskopPath to quoted form of (POSIX path of (choose folder))
repeat with aFile in soughtFiles
	set spotlightquery to "\"kMDItemFSName == " & aFile & "\""
	set command to "mdfind -onlyin " & deskopPath & " " & spotlightquery
	set end of foundFiles to paragraphs of (do shell script command)
end repeat

You can use the or operator “||”

Craig & Adam,

Thanks to both, with your code/s, i am able to search 2 files. Both options work fine. GREAT! :slight_smile:

How about the other part, where i have to generate a text or csv file based on the search result.
Example report:
/users/john/job1/ abc1 Available
/users/john/job1/ abc2 Available
/users/john/job2/ abc1 Available
/users/john/job2/ abc2 Not Available
/users/Tom/job1/ abc1 Not Available
/users/Tom/job1/ abc2 Not Available

Thanks in advance
JaiM

Hello

I have a different solution which gives you the report you wanted, but a bit slower. :slight_smile:
Should you need something to create your initial pathname with easily: http://macscripter.net/viewtopic.php?pid=128501#p128501

property theOuterList : {}
property theInnerList : {}
property filereport : "File Report:" & return
property parentFolder : {}
property filelist : {"abc1", "abc2"}
tell application "Finder"
        set usersFolder to a reference to "Macintosh HD:Users:"

	set theOuterList to every folder of folder usersFolder
	repeat with parentFolder in theOuterList
		set theInnerList to every folder of folder (parentFolder as text)
		repeat with childFolder in theInnerList
			tell childFolder
				set pu to POSIX path of (childFolder as alias)
				
				repeat with theFile in the filelist
					try
						set f to (a reference to file theFile) as alias
						set mf to pu & tab & theFile & tab  & " -- Available"
						set filereport to filereport & mf & return
					on error e number n
						set mf to pu & tab & theFile & tab & "-- Not Available"
						set filereport to filereport & mf & return
					end try
				end repeat
			end tell
		end repeat
	end repeat
end tell
filereport

Best Regards

McUsr

Hi,

there are a lot of long-winded useless coercions.
Referencing with a reference to is not needed at all, for example a reference to a literal string is a literal string. Referencing can be useful for “real” objects like files or folders.

In this lines

set ph to (a reference to (contents of childFolder) as alias) as text
set pu to POSIX path of ph as text

reference, contents and both as text coercions are not needed.
This is sufficient, a POSIX path is always text

set ph to POSIX path of (childFolder as alias)

.

Hello

Thanks for reviewing it Stefan.

I’ll rewrite it. I didn’t see that. -I just meshed together something.

Best Regards

McUsr

Thanks McUser. I have used your code and it works perfect. Exactly what i am looking for.

StefanK - I have modified the code as you have recommended.

But now i have a new challenge, users started creating files with names like abc1a, abc2a, abc2b).

Is there a way to address the above situation?

Can we modify the below line to say something like file name begins with abc1, abc2?
property filelist : {“abc1”, “abc2”}

Thanks in advance.

Hello
I have changed it to reflect your wishes, hopefulyl in a way that you can use.
I now regards the former “filelist” as an “entitieslist”, and the physical files as items.
I have sorted the files by name, so it is important that users use a versioning scheme that doesn’t break the sorting order. For instance: if there can be more than 10 abc’s then the numbering should start with 01 and not 1.

You better be patient when running this since it has been a real resource hog. I consider splitting the script into two.

  1. A “fast” report that just tells if there are any files available (items) for the entity.
  2. A detailed list.

Another option is to just return the latest item an entity (read last)

It would be interesting to know how may users you have and how long time it takes to execute.

Hopefully Stefan et. al has some clever tricks (or better knowledge than I) on how to speed things up a little bit.
I think a faster version would delegate much of the work to a shell script.

I hope you can use it after all.

Best Regards

McUsr

As corrected by StefanK 01.06.2010

Hopes this helps.

property theOuterList : {}
property theInnerList : {}
property filereport : "File Report:" & return
property parentFolder : {}
property entitiesList : {"abc1", "abc2"}
property documentFileList : {}
property itemsList : {}
tell application "Finder"
	set usersFolder to a reference to "Macintosh HD:Users:"
	
	set theOuterList to every folder of folder usersFolder
	repeat with parentFolder in theOuterList
		set theInnerList to every folder of folder (parentFolder as text)
		
		repeat with childFolder in theInnerList
			tell childFolder
				-- was: set documentFileList to (every file sort by name)
				set documentFileList to sort (get every file) by name -- Corrected by StefanK
				-- was: set pu to POSIX path of (childFolder as alias)
				set pu to POSIX path of (it as alias) -- Corrected by StefanK
				
				repeat with theFile in the entitiesList
					set itemsList to {}
					repeat with aDocFile in documentFileList
						set docNm to (name of aDocFile)
						if docNm contains theFile then
							set end of itemsList to (name of aDocFile)
						end if
					end repeat
					if (count of itemsList) is not 0 then
						set mf to pu & tab & theFile & tab & " -- Available"
						set filereport to filereport & mf & return
						repeat with docNm in the itemsList
							set mf to pu & tab & docNm -- & tab & "
							set filereport to filereport & mf & return
						end repeat
					else
						set mf to pu & tab & theFile & tab & "-- Not Available"
						set filereport to filereport & mf & return
					end if
				end repeat
			end tell
		end repeat
	end repeat
end tell
beep 5
tell application "TextEdit"
	make new document
	set the text of document 1 to filereport as text
end tell

Hello McUser,

Thanks once again and sorry for the delayed reply.

When i tried to run the script that you have given, i am getting an error message "Finder got an error: every file of item 1 of {folder “xxx xxx xxx (xxx - folder path)”} doesn’t understand the sort message.

Am i supposed to install any additional OSAX? I am running this script on 10.5.8.

Hello

I wouldn’t think I have used any capability not available in Leopard.

I tested this when I wrote it, and it ran properly on folders which contained files or not.

Does this happen to every folder or just to some particular folder.

Could you please give me the exact error message with the number?

The only other thing I can think of at the moment is that maybe you lack some rights.
That is if this doesn’t happen to every folder then I suggest you review that particular folder for oddities i.e
if there is something about its rights or its contents, not necessarily the files you are after.
I believe this could happen if there were file in the folder for which you have no access rights.

That it should be an error in this script - it is very possible :frowning: It maybe. Event though it ran nicely.
The sort command isn’t anything new.
And are you sure your script is totally original?

Does is it run properly when you run it directly from the script-editor?
I think I only ran it directly from the script editor.

Best Regards

McUsr

Hi,

a more reliable syntax to sort files is

set documentFileList to sort (get every file) by name

By the way: it’s quite dangerous to use a large tell block targetting a folder reference.
For example the line

set filereport to filereport & mf & return

is actually treated as

set filereport of childfolder to filereport of childfolder & mf & return

In this case it’s no problem because a string is no reference, but it could cause unexpected errors

As far as I can see the reference is only used in the first line.
In the second line there is a double reference (tell childfolder - of childfolder) which is also strongly recommended to be avoided

The correct syntax for the second line with the tell block is

set pu to POSIX path of (it as alias)

Hello and thank you very much Stefan

I learn a lot about the details of you; the important details.
I’ll change it immediately! -I do so in the post above.
As Stefan said, the filereport usage can’t do any harm, so I won’t change that.

I’ll think about double references for the rest of the day!

Best Regards

McUsr

Hi McUser & StefanK,

Again million thanks to both.

In my earlier mail, i have mentioned that it didn’t work. Later i realized that my mac had some problem, which caused the issue. After fixing my mac, it works fine.
:smiley: