Purge Script

I am trying to select a folder to purge from, select a text document with a list of items to purge, then either select a folder to move the files to, or send the files straight to the trash.

Below Is what I have so far. It half works. It deletes the files from the list, but then it’s last action is to delete the original folder chosen to purge.

I am sure I am missing something simple, I think I just need a second set of eyes.

Thanks in advance for any help you can provide.



tell application "Finder"
	
	--Select folder with files to Purge
	set folderofiles to choose folder with prompt "Select the folder of files you want to purge."
	
	--Choose txt file list of files to be deleted
	set filelist to choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
	--set finaldestination to choose folder with prompt "Select the directory these files will be moved to."
	
	--Present choice of choosing a folder or sending to trash
	set question to display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2
	set answer to button returned of question
	
	if answer is "Browse..." then
		set finaldestination to choose folder with prompt "Select the directory these files will be moved to."
	end if
	
	if answer is "Trash" then
		set finaldestination to path to trash
	end if
	
	tell application "Finder"
		
		set the_list_count to paragraphs of (read filelist from 1)
		
		repeat with i from 1 to (count the_list_count)
			
			try
				
				set filepath to POSIX path of folderofiles
				set destination to POSIX path of finaldestination
				set filename to item i of the_list_count
				set filetomove to filepath & filename
				
				
				do shell script "/bin/mv  '" & filetomove & "' '" & destination & "'"
				
			end try
			
		end repeat
		
		display dialog "Purge is complete"
		
	end tell
	
	if finaldestination is (path to trash) then
		empty trash
	end if
	
end tell

Model: macbook pro
AppleScript: 2.61
Browser: Firefox 32.0
Operating System: Mac OS 9.1.x

Hi,

if the contents of the text file ends with a LF or CR character, the read command treats it as a paragraph containing an empty string. Then the parent folder is deleted.

As you empty the trash anyway, the Finder is not needed at all
This version of the script checks the length of the paragraph


--Select folder with files to Purge
set folderofiles to POSIX path of (choose folder with prompt "Select the folder of files you want to purge.")

--Choose txt file list of files to be deleted
set filelist to choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
--set finaldestination to choose folder with prompt "Select the directory these files will be moved to."

--Present choice of choosing a folder or sending to trash
set {button returned:answer} to display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2

if answer is "Browse..." then
	set finaldestination to POSIX path of (choose folder with prompt "Select the directory these files will be moved to.")
else
	set finaldestination to missing value
end if

set fileNames to paragraphs of (read filelist)

repeat with aFile in fileNames
	if length of aFile > 0 then
		try
			set filetomove to folderofiles & aFile
			if finaldestination is missing value then
				do shell script "/bin/rm " & quoted form of filetomove -- deletes the file immediately
			else
				do shell script "/bin/mv " & quoted form of filetomove & space & quoted form of finaldestination
			end if
		end try
	end if
end repeat

display dialog "Purge is complete"

Now the script isn’t deleting anything in the folder. It looks like it is adding a \ to the end of the file name in the text file…here’s the event.

tell current application
read alias “CRP-IT-10010:Users:thumser:Desktop:wMactive-RC.rtf”
→ "{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf210
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww12600\viewh7800\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural

\f0\fs24 \cf0 337624-01.PDF\
337641-01.PDF\
364023-01.PDF\
364963-01.PDF\
365197-01.PDF\
367776-01.PDF\

the read command expects a plain text file (.txt)

Still not moving anything. Events below.

choose folder with prompt "Select the folder of files you want to purge."
	--> alias "CRP-IT-10010:Users:thumser:Desktop:PURGE:"
choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
	--> alias "CRP-IT-10010:Users:thumser:Desktop:wMactive-RC.txt"
display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2 given «class Krtn»:{button returned:"answer"}
	--> {button returned:"Browse..."}
choose folder with prompt "Select the directory these files will be moved to."
	--> alias "CRP-IT-10010:Users:thumser:Desktop:Remove:"

end tell
tell current application
read alias “CRP-IT-10010:Users:thumser:Desktop:wMactive-RC.txt”
→ "337624-01.PDF
337641-01.PDF
364023-01.PDF
364963-01.PDF
365197-01.PDF
367776-01.PDF
368267-01.PDF
368993-01.PDF
369039-01.PDF

373707-01.PDF
373709-01.PDF
373772-01.PDF
"
end tell
tell application “AppleScript Editor”
display dialog “Purge is complete”
→ {button returned:“OK”}
end tell
Result:
{button returned:“OK”}

I just tested the script and it works fine.

However I edited the post a few minutes after posting, please check the current version

Got it!!

needed to change

set filetomove to filepath & aFile

to

set filetomove to folderofiles & aFile

Working script posted below. Thanks for your help Stefen :slight_smile:


--Select folder with files to Purge
set folderofiles to POSIX path of (choose folder with prompt "Select the folder of files you want to purge.")

--Choose txt file list of files to be deleted
set filelist to choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
--set finaldestination to choose folder with prompt "Select the directory these files will be moved to."

--Present choice of choosing a folder or sending to trash
set {button returned:answer} to display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2

if answer is "Browse..." then
	set finaldestination to POSIX path of (choose folder with prompt "Select the directory these files will be moved to.")
else
	set finaldestination to missing value
end if

set fileNames to paragraphs of (read filelist)

repeat with aFile in fileNames
	if length of aFile > 0 then
		try
			set filetomove to folderofiles & aFile
			if finaldestination is missing value then
				do shell script "/bin/rm " & quoted form of filetomove -- deletes the file immediately
			else
				do shell script "/bin/mv " & quoted form of filetomove & space & quoted form of finaldestination
			end if
		end try
	end if
end repeat

display dialog "Purge is complete"