Delete all items in a folder

Hello everyone and thank u for taking the time to help me.

Well, this is my first time working with applescript, I got the logic but I’m having some trouble with the syntax. I been working on a script that would delete all the items in a folder. I got it to do so but I run into problems with the locked files, I looked around and found the way to unlock the files, now I need to unlock files within other folders I know I have to use a recursive function so it can go in to multiple levels but i’m running into problems when I check to see if the item is a folder or just a file. This is my code at the moment, is not all of it and I’m just going one step at a time. The error that I keep on getting is “Finder got an error : File folder Folder 1 wasn’t found.” Folder 1 is one of the folders inside of the folder I want to delete the items from. Can anyone plz show me and explain what is it that I’m doing wrong thnx in advance.

X@vier


tell application "Finder"
	set x to alias �
		((startup disk as string) & ":Macintosh HD:Users:macadmin:Desktop:script_test_folder")
	
	repeat with index from 1 to the count of x
		set currentItem to (item index of x)
		
		set the currentItemInfo to info for currentItem
		if folder of the currentItemInfo is true then
			
			display dialog " This is Folder " & currentItemInfo
			
		end if
		
		
	end repeat
end tell

:lol:

Maybe this will work.

tell application "Finder"
	set x to folder "script_test_folder" of desktop
	set locked of (files of entire contents of x) to false
	delete entire contents of x
end tell

– Rob

WOW buddy that worked gr8 thnx. Can u plz explain to me if u can y I was getting that error msg.

X@vier.

It might have been because of this line:

set x to alias ((startup disk as string) & ":MacintoshHD:Users:macadmin:Desktop:script_test_folder")

If the name of your startup disk is MacintoshHD, it was included twice in the path.

– Rob

Hello Rob

I removed the (MacintoshHD) part from the code and still didn’t work :(, I keep getting the same error msg.
I tried the code that u gave me and it worked gr8 when I tested it, as soon as I put it in the startupitem folder and logout and login I get an error msg saying that xxxx (any file) file is locked. Any ideas what can be causing this error?

Thnx in advance

X@vier

First, I suggest that, if it isn’t, the script should be the last item in your list of Login Items. There’s a lot going on when you log in and it’s possible that all of the login activity is interfering with the execution of the script.

Second, maybe this will help to overcome the error that you are encountering.

tell application "Finder"
	set x to folder "script_test_folder" of desktop
	set locked of (files of entire contents of x) to false
	update (files of entire contents of x)
	delete entire contents of x
end tell

– Rob

Hello Rob

Well, is finally working thnx for all ur help and this is the final code that I’m using for anyone that may need it.


tell application "Finder"
	set FOLDERwithITEMS to alias ¬
		((startup disk as string) & ":Users:student:Documents")
	
	if (count of FOLDERwithITEMS) < 1 then
		display dialog "nothing in Folder"
		empty
		
	else
		set locked of (files of entire contents of FOLDERwithITEMS) to false
		update (files of entire contents of FOLDERwithITEMS)
		delete entire contents of FOLDERwithITEMS
		empty
		
	end if
end tell

again thnx for all ur help

Glad to help. :slight_smile:

– Rob