Mdfind and delete
Mdfind and delete
- Subject: Mdfind and delete
- From: Nellyann Rosario <email@hidden>
- Date: Fri, 23 Oct 2009 18:46:45 -0400
- Thread-topic: Mdfind and delete
Title: Mdfind and delete
I used this script last year but now is not working. Basically what it does searches for the 3 letter code in the “list” and if the 3 letter code is found in the server then creates a log & deletes the files from the chosen folder. I’m running it from ScriptEditor2.2.1 and it seems that is finding the codes in the list but it not deleting the files & neither creates a log. I checked the permissions in the folder & I can read & write. Can someone guide me in the right direction? I’m using Leopard 10.5.8 .
--use mdfind to find all files that contain letter codes
set lookInThisFolderOnly to choose folder with prompt "Choose folder that has images to delete"
set logFolderPath to (choose folder with prompt "Choose a place to write the log") as string
set lookInThisFolderOnly to (POSIX path of lookInThisFolderOnly)
set theCodeList to {"AAN", "ACG", "ADH", "AEC", "AEU", "AFE", "AGP", "AGR", "AHB", "AHM", "AHO", "AHR", "AIL", "AIX", "AK1", "AKY", "ALI", "ALV", "ALW", "AME", "AMN", "APE", "APP", "APQ", "APS", "APU", "APX", "AQX", "ARE", "ARL", "ARM", "ARN", "ART", "ARV", "ARY", "ASH", "ASW", "ATL", "ATR", "AVK", "AWK", "AYH", "AZ1", "AZ2", "AZR", "BAD", "BAM", "BAR", "BAW", "BBL", "BBV", "BCC", "BCH", "BCV", "BCY", "BDH", "BDS", "BDY", "BED", "BEH", "BEV", "BFT", "BGA", "BGB", "BGD", "BGX", "BHG", "BHS", "BIF", "BIN", "BIO", "BKS", "BLK", "BLM", "BLO", "BLQ", "BLR", "BLS", "BMT", "BNE", "BNL", "BNT", "BOB", "BOD", "BOH", "BOO", "BPC", "BRA", "BRY", "BSC", "BSE", "BSK", "BSN", "BSR", "BSS", "BTF", "BTS", "BTV", "BUS", "BVC", "BVP", "BWO", "BWR", "BZY", "CAI", "CBC", "CBF", "CBV", "CBX", "CCC", "CCE", "CCG", "CCZ", "CDP", "CEB", "CEC", "CEE", "CEM", "CEX", "CFA", "CFF", "CFL", "CFZ", "CGA", "CGH", "CGV", "CHB", "CHL", "CHQ", "CHS", "CIB", "CIM", "CIX", "CLB", "CLD", "CLH", "CLR", "CMO", "CNC", "COA", "COB", "COD", "COK", "COT", "CPE", "CPG", "CPW", "CQL", "CRV", "CSE", "CSX", "CTB", "CTC", "CTD", "CUV", "CVE", "CVN", "CVO", "CVQ", "CXM", "CXP", "DAK", "DAS", "DBC", "DBW"}
repeat with aCode in theCodeList
set theMDFindScript to "mdfind -onlyin " & lookInThisFolderOnly & " kMDItemDisplayName == '*" & (aCode as string) & "*'"
set theList to (do shell script theMDFindScript)
if theList ≠ "" then
tell me to writeToFile({rpText:("- Files Deleted - " & return & theList & return), rpDest:logFolderPath, rpFileName:"DeleteFiles.log", opAppend:"a", opTimeStamp:"f"})
set AppleScript's text item delimiters to return
set theList to every text item of theList
tell me to deleteFileFolder({rpTarget:theList})
end if
end repeat
--===========Library DeleteFileFolder======
on deleteFileFolder(theParamList) --all worker processed scripts need "doScript" as handler.
try -->>main library block --general fault protection
set errMsgText to "" --this can be used for non-terminal errors to return back to the script, if needed
try -->>required block --verify that required parameters are available
set rpTarget to (rpTarget of theParamList)
on error errMsg --required block
--terminal error, exit and return error
return "Error: library_Template:deleteFileFolder(theParamList) required parameter missing - " & errMsg
end try --required block
try -->>set optional parameters
set opContentsOnly to (opContentsOnly of theParamList)
on error
set opContentsOnly to false --set to default value if necessary
end try
-->>
if opContentsOnly = true or opContentsOnly = "true" then
set targetList to {}
repeat with aTarget in rpTarget
set theTargetContents to (list folder aTarget)
repeat with aTargetContent in theTargetContents
set posixTarget to (quoted form of (POSIX path of (aTarget & aTargetContent)))
copy posixTarget to end of targetList
end repeat
end repeat
set AppleScript's text item delimiters to " "
set targetList to (targetList as string)
if targetList is not equal to "" then
set theShellScript to "rm -r " & targetList
do shell script theShellScript
end if
else
set targetList to {}
repeat with aTarget in rpTarget
set posixTarget to (quoted form of (POSIX path of aTarget))
copy posixTarget to end of targetList
end repeat
set AppleScript's text item delimiters to " "
set targetList to (targetList as string)
set theShellScript to "rm -r " & targetList
do shell script theShellScript
end if
--<<--
on error errMsg --main library block
set errMsgText to (errMsgText & errMsg & ">> Problem running library." & return)
end try --main library block
set AppleScript's text item delimiters to ""
if errMsgText ≠ "" then
return errMsgText
else
return 0 --if it makes it this far, then all is well (I hope!)
end if
end deleteFileFolder
--========Libary WriteToFile======
on writeToFile(theParamList)
try -->>main library block --general fault protection
set errMsgText to "" --this can be used for non-terminal errors to return back to the script, if needed
set theDateString to "" --used for containing results from a shell script
try -->>required block --verify that required parameters are available
rpText of theParamList
rpDest of theParamList
rpFileName of theParamList
on error errMsg --required block
return "Error: library_WriteToFile:writeToLogFile(theParamList) required parameter missing - " & errMsg
end try --required block
try -->>set optional parameters
set opTimeStamp to (opTimeStamp of theParamList)
on error
set opTimeStamp to "" --set to default value if necessary
end try
try
set opAppend to (opAppend of theParamList)
on error
set opAppend to "a" --set to default value if necessary
end try
try -->> format date block
if opTimeStamp ≠ "" then --add date stamp?
if opTimeStamp contains "f" then --f (full overrides other formats)
set dateCommand to "date" --full date
else
set dateCommand to "date +"
repeat with aTimeOption in opTimeStamp of theParamList
if aTimeOption as string = "t" then
set dateCommand to dateCommand & "_%H:%M:%S_" --wrap time into one format command
else
set dateCommand to dateCommand & "%" & aTimeOption & "/" --pass parameters into format command
end if
end repeat
end if
if dateCommand ≠ "" then
set theDateString to (do shell script dateCommand) & tab --shell command is easier to format than applescript
end if
end if
on error errMsg -- format date block
--terminal error, exit and return error
return "Error: library_WriteToFile: problem formatting datestamp - " & errMsg
end try -- format date block
set theFile to ((rpDest of theParamList) & (rpFileName of theParamList))
try -->>write block
try
close access file theFile --in case it was open
end try
open for access file theFile with write permission -- open the file to write
if opAppend = "r" then
set eof of file theFile to 0 --empty all data
end if
write (theDateString & (rpText of theParamList) as text) to file theFile starting at eof --write the data to the file
close access file theFile --close the file
on error errMsg --write block
--terminal error, exit and return error
try
close access file theFile --close the file
end try
return "Error: library_WriteToFile: problem writing the file - " & errMsg
end try --write block
on error errMsg --main library block
set errMsgText to (errMsgText & errMsg & ">> Problem running library." & return)
end try --main library block
set AppleScript's text item delimiters to ""
if errMsgText ≠ "" then
return errMsgText
else
return 0 --if it makes it this far, then all is well (I hope!)
end if
end writeToFile
_____________________________________________________________________________
Scanned by IBM Email Security Management Services powered by MessageLabs. For more information please visit http://www.ers.ibm.com
_____________________________________________________________________________
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden