Remove item from a list

I have a list of filenames from a particular folder.

I am comparing the list of filenames with records of a database. Essentially I am looking to find if file 1.jpg is specified in my data. If it is, then I want “1.jpg” removed from the list. The comparison portion of things is done. Now, I am simply looking to remove that item from the list, leaving me a list of files that were not utilized in the database.

Does anyone know how to accomplish this?

You may be interested in this: How can I delete items from a list?

jj’s article, to which Bruce refers, shows several good ways to “remove” items from lists and mentions that it’s often possible to invent faster ways, depending on the circumstances. If you have a list of items that are all of the same class ” such as a list of file names ” it’s possible to lose particular ones by replacing them with items of a different class. (I normally use ‘missing value’.) Then, when you’ve looped through the list, you simply reset the list variable to the remaining strings (or Unicode texts, or whatever).

repeat with i from 1 to (count fileNames)
	if (item i of fileNames is in myData) then
		set item i of fileNames to missing value
	end if
end repeat
set fileNames to fileNames's strings
-- or: set fileNames to fileNames's every Unicode text

Nigel this looks like it would work great - in my situation there will only be one instance of each string… is it possible to exit the repeat loop once the if statement is true?

In other words, is it possible that when item i = a.jpg and a.jpg is in myData, can you immedately exit the repeat or do you have to continue up to the count of fileNames?

Hi, Scott. Yes indeed. Just insert ‘exit repeat’ before the ‘end if’.

repeat with i from 1 to (count fileNames)
	if (item i of fileNames is in myData) then
		set item i of fileNames to missing value
		exit repeat
	end if
end repeat
set fileNames to fileNames's strings
-- or: set fileNames to fileNames's every Unicode text

repeat with i from 1 to (count allFiles)
	if (item i of allFiles is (OrderID & ".jpg")) then
		set item i of allFiles to missing value
		exit repeat
	end if
end repeat
set allFiles to allFiles's strings

This is what I have - where allFiles is the list of all files in the folder (minus invisibles) & OrderID.jpg is the file that I am looking for.

When I run this the first time, and return allFiles after the "set allFiles to allFiles’s strings command, I get {}.

Any clue as to what I am doing wrong here?

From JJ’s article, I implemented the following code which seems to work well.


set itemsToDelete to {(OrderID & ".jpg")}
set cleanList to {}
repeat with i from 1 to count allFiles
	if {allFiles's item i} is not in itemsToDelete then set cleanList's end to allFiles's item i
end repeat
set allFiles to cleanList

The list folder command, from Scripting Additions’ File Commands, returns a list of Unicode text, Scott. You therefore seem to be trying to extract the wrong class from the list.

Try changing your last line to incorporate the alternative extraction method that Nigel suggested: