Delete trash and other hidden folders from usb-stick

Hi,

a few days ago I “discovered” AppleScript and it looks really nice!
I just played around with folder actions and got stuck with iterating through the folders of a selected disk in Finder.

I just want to remove the hidden trash-folders from the selected Volume and then eject it.
Ejecting the selected items disk is really easy, but I don’t find some “easy” functions to find these hidden folders!?

Is there a way to search for folders?
Something like ‘delete any folder starting with “.” and containing “trash”’?! :slight_smile:

Here is my script: (Please don’t wonder about the syntax, I think there are many better ways to realize that)



tell application "Finder" to repeat with MyItem in (get selection)
	
	set thedisk to disk of MyItem
	set isEjectable to ejectable of thedisk
	set diskname to name of thedisk
	
        --Only check ejectable disks
	if isEjectable then
		
		-- Security dialog
	 
		display dialog "Delete all hidden stuff  from " & diskname & "?" buttons {"No", "Yes"} default button 2
		if button returned of result = "Yes" then

			-- Iterate through all folder of thedisk and delete every hidden folder that's starting with ".", containing "Trash" 
			-- ????? somethings going very wrong here...

			repeat with i in folder of thedisk
				display dialog "name = " & (get name of i as string)
			end repeat
			
			--Eject Disk		
			eject thedisk
			beep
			
		end if
	end if
end repeat



Thanks in advance
Jan

I haven’t tested this so you may need to tweak it a little, but it should get you close to what you want. If you have questions about any of the code just ask…

tell application "Finder"
	set theSelection to item 1 of (get selection)
	set selectedDisk to disk of theSelection
	if ejectable of selectedDisk is true then
		set thisDisk to selectedDisk as text
		set allFoldersList to every folder of entire contents of folder thisDisk
		repeat with j from 1 to (count of allFoldersList)
			set thisFolder to (item j of allFoldersList as text) & ".Trash"
			try
				do shell script "rm " & quoted form of POSIX path of thisFolder
			end try
		end repeat
	end if
end tell

hi regulus,

thank you very much for your answer! It works perfectly fine! :slight_smile:
As an addition I’ve to delete some folders in the Root, but with your code it’s really easy to do this!
Thanks again!
Jan

me again…

While studying your code, I thought about a more easy way to clean up. There are many more System files stored from OS X. We have “.Trashes”, “._.Trashes”, “.Spotlight-V100” and sometimes deleted files like “.MyDeletedFile.pdf”.
So I thought about a recursive delete (“rm -rf” ???) with a Wildcard. This recursive delete should delete anything starting with a “.”. I thought “.*” as the filename for the rm-shell-script would do the job, but nothing happens.


tell application "Finder"
	set theSelection to item 1 of (get selection)
	set selectedDisk to disk of theSelection
	if ejectable of selectedDisk is true then
		set thisDisk to selectedDisk as text
		
                --Delete all these folders - this works fine
		--set deleteRoot to {thisDisk & "._.Trashes", thisDisk & ".Trashes", thisDisk & ".Spotlight-V100"}
		--repeat with i from 1 to number of items in deleteRoot
		--	set this_item to (item i of deleteRoot as text)
		--	try
		--		do shell script "rm -fr " & quoted form of POSIX path of this_item
		--	end try
		--end repeat
		
                --TEST: WILDCARD NOT WORKING!
		try
			do shell script "rm -fr " & quoted form of POSIX path of (thisDisk & ".*")
		end try
		
		eject selectedDisk
		beep
	end if
end tell

So what is wrong with this Script???

Thanks
Jan

Hi,

the quotation of the whole path breaks the wildcard, try


			do shell script "rm -fr " & quoted form of POSIX path of thisDisk & ".*"

this quotes only the path to the disk

That’s it!!!
Thank you very much!

So here is the full script, for those of you that need an easy way to clean up an usb-stick while ejecting it.


tell application "Finder"
	set theSelection to item 1 of (get selection)
	set selectedDisk to disk of theSelection
	if ejectable of selectedDisk is true then
		set thisDisk to selectedDisk as text
		
		try
			do shell script "rm -fr " & quoted form of POSIX path of thisDisk & ".*"
		end try
		
		eject selectedDisk
		say "USB-Stick was successfully cleaned up and ejected. If life would be that easy!"
	else
		say "That's not an ejectable Volume. Please select an USB-Stick in Finder"
	end if
end tell

Jan