Paths to multiple files/folders

Ok, so I wrote a script to search a hard drive on the network for specific text within the names of the files and folders on the drive. It took me a while to get it to work properly because of references to files and folders. The following script works (on my computer) but I want to know if there is a different way to approach this. My script uses loops and has to search through several hundred items and the list will only get larger as time moves forward. The loops of course get quite slow after a while. So any way, if you all don’t mind, please take a look at this script and let me know if there is a different way about it.

set theList to {}
set textList to {}
display dialog "What folder/file name text do you wish to find?" default answer ""
set theText to the text returned of the result
tell application "Finder"
	if not (exists disk "OWC Neptune") then
		mount volume "afp://usrName:pasWord@155.155.155.155/theHardDrive"
	end if
	if not (exists folder "Retrieval Unit" of desktop)  then
		make new folder at desktop with properties {name:"Retrieval Unit"}
		set label index of folder "Retrieval Unit" to 6
	end if
	set theDisk to disk "theHardDrive"
	repeat with anItem in theDisk -- selecting a folder or file
		if class of anItem is folder then
			repeat with theItem in anItem -- selecting an item of a folder of the disk
				set thePath to theItem as alias
				if name of theItem contains theText then
					set theList to theList & thePath -- when text is found in the name of the item, make a list containing the path to the item.
					set textList to textList & name of theItem -- make a list of the names of items containing the text search.
				end if
			end repeat
		else if class of anItem is file then
			if name of anItem contains theText then
				set theList to theList & thePath
				set textList to textList & name of anItem
			end if
		end if
	end repeat
	set theChoice to choose from list textList with prompt "Make your choices." with multiple selections allowed -- choosing the files/folders to be retrieved -- use the textList to select the files/folders that match the criteria searched for
	repeat with theItem in theList
		if theChoice contains name of theItem then
			move theItem to folder "Retrieval Unit" of desktop
		end if
	end repeat
end tell

Thanks everyone.

PreTech

Model: G5 dual 1.8
AppleScript: 2.1
Browser: Safari 125.12
Operating System: Mac OS X (10.4)

You can use recursion (a subroutine that calls itself) to easily navigate through an n-deep directory tree. Search this board for examples. This sounds really slow. Would a finder find or spotlight be of any use for this?

Yes I can use spotlight or find to get the info but it (as far as I’m aware) can’t automatically move the selected files and folders for me. Also this is more of a piddling around to learn more about scripting. This script also only searches one folder deep which is all I really need for my purposes (of course the info for searching n-deep will be nice to know:cool:). I tried to use the line:

set myVariable to every item of folderX whose name contains theText

and this works but I could not figure out how to then make a choose from list statement that would return values that were paths to the items. If I made my choices and then the script was supposed to move those choices it would error saying something like "could not make item such & such into expected type. So the short question really is how do I create a list of references that I can choose from, select multiple, and then move the selections to my destination? In this script I had to make two lists, one for the references to the items and one for the names of the references that could be chosen.

Thanks for your help.

PreTech

Model: G5 dual 1.8
AppleScript: 2.1
Browser: Safari 125.12
Operating System: Mac OS X (10.4)

I’m a bit preoccupied today, so this is a hasty reply.

property theDisk : "/Volumes/SOPRANOS_DISC4"

repeat
	display dialog "What folder/file name text do you wish to find?" default answer ""
	set theText to the text returned of the result
	if result is not "" then exit repeat
end repeat

tell application "Finder"
	launch
	(*if not (exists disk "OWC Neptune") then
		mount volume "afp://usrName:pasWord@155.155.155.155/theHardDrive"
	end if*)
	
	if not (exists folder "Retrieval Unit" of desktop) then
		make new folder at desktop with properties {name:"Retrieval Unit"}
		set label index of folder "Retrieval Unit" to 6
	end if
end tell

do shell script "find " & quoted form of theDisk & ¬
	" -iname " & quoted form of ("*" & theText & "*") & " 2> /dev/null"

choose from list (paragraphs of result) with prompt "Make your choices." with multiple selections allowed

repeat with thisItem in result
	do shell script "cp " & quoted form of thisItem & " " & quoted form of POSIX path of ((path to desktop as text) & "Retrieval Unit")
end repeat