Getting a valid path back from terminal find command.

Hello,
Basically I’m trying to put up a dialog that uses the terminal find command to find a file, if more than one item with the same name is found it will display a choose from list dialog, and then delete the file and its parent directory. As usual, I don’t quite know enough to actually make the the thing work. :rolleyes:

Anyway, here’s the script I’m hacking together, the directory being searched is a Windows Server. I noticed when it returns results in the choose file dialog it adds an extra / to path (like, parent//child/child) which is weird. I don’t know what’s going on there yet.

I know this is messy…

Any help appreciated,
Aaron



property kDir : ""

if kDir = "" then
	set kDir to choose folder
end if

set p to POSIX path of kDir
log p

set theResponse to display dialog "Which File to Delete?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

set kFileName to text returned of theResponse

set kPath to "find " & quoted form of p & " -name " & kFileName
log kPath

set killFiles to do shell script kPath


set myChoice to killFiles
choose from list KillFiles

--need to rm the parent directory of the chosen item(s)



Hi. Welcome to MacScripter.

The extra slash happens because find inserts a slash between the root path it’s given and each subpath it finds. Since the root path here is a POSIX path derived from a folder alias, it already ends with a slash, so two slashes appear together in the results. But they behave as if they were one and nothing needs to be done about them.

The result from the shell script is a single text with one found path on each line, so you need a list of the text’s paragraphs for choose from list. You should probably check that something’s actually been found before invoking choose from list. If you think you may need to choose more than one of the items in the list for deleting, you’ll need to use choose from list’s multiple selections allowed parameter.



property kDir : ""

if kDir = "" then
	set kDir to choose folder
end if

set p to POSIX path of kDir
log p

set theResponse to display dialog "Which File to Delete?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

set kFileName to text returned of theResponse

set kPath to "find " & quoted form of p & " -type f -name " & quoted form of kFileName
log kPath

set killFiles to do shell script kPath

if (killFiles is "") then return -- or show an error message (file name not found)

set killFiles to paragraphs of killFiles
set myChoice to (choose from list killFiles with multiple selections allowed)
if (myChoice is false) then return -- 'choose from list' returns 'false' if its Cancel button's used.

repeat with thisItem in itemsToKill
	
	--need to rm the parent directory of the chosen item(s)
	-- (Strip the file name from each chosen path and rm the result)
end repeat