Need A Script to Check For Missing Files/Broken Serial # Ranges

That result is not possible. It is a bug in your environment or OS.
text -8 thru -5 = .999
.999 as integer is 1

I’m not sure how helpful any of our replies have been to liatmgat. None of our scripts are written in a way that an obvious newbie can easily understand and most of them don’t do exactly what was asked.

Here’s an attempt at a more helpful, understandable, and yet still fast, vanilla script:

set theFolder to (choose folder)

try
	-- Just tell System Events to return all the file names in the folder. The script itself can filter for extensions much faster.
	tell application "System Events" to set theNames to name of every file of folder "2370x1800" of theFolder
on error errMsg
	display dialog errMsg buttons {"Stop"} default button 1 cancel button 1 with icon stop
end try

set theNumbers to {} -- A collector for the serial numbers from the names
set firstHit to true -- "First hit" flag. Certain variables will be initialised on the first ".dpx" hit, updated thereafter.

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."

try
	-- There's no need to sort the names. The lowest and highest numbers can be determined as they're extracted.
	repeat with i from 1 to (count theNames)
		set thisName to item i of theNames
		if (thisName ends with ".dpx") then
			-- If this name ends with ".dpx", extract and store the serial number from it.
			set thisNumber to (text item 2 of thisName) as integer
			set end of theNumbers to thisNumber
			if (firstHit) then
				-- On the first ".dpx" hit, get the base name and initialise the "highest" and "lowest" number variables.
				set baseName to (text item 1 of thisName) & "."
				set highestNumber to thisNumber
				set lowestNumber to thisNumber
				set firstHit to false
			else
				-- On subsequent hits, update the highest or lowest number.
				if (thisNumber > highestNumber) then
					set highestNumber to thisNumber
				else if (thisNumber < lowestNumber) then
					set lowestNumber to thisNumber
				end if
			end if
		end if
	end repeat
on error errMsg
	set AppleScript's text item delimiters to astid
	display dialog errMsg buttons {"Stop"} default button 1 cancel button 1 with icon stop
end try
set AppleScript's text item delimiters to astid

if (theNumbers is {}) then
	display dialog "No .dpx files in the folder!" buttons {"Stop"} default button 1 cancel button 1 with icon stop
end if

set missingNames to "Files missing:"
repeat with thisNumber from (lowestNumber + 1) to (highestNumber - 1)
	if (thisNumber is not in theNumbers) then
		set expectedName to baseName & thisNumber & ".dpx"
		set missingNames to missingNames & linefeed & expectedName
	end if
end repeat

if (missingNames ends with ".dpx") then
	display alert "Broken Frames Detected!" message missingNames buttons {"OK"} default button 1
else
	display dialog "No breaks in the file name sequence." buttons {"OK"} default button 1 with icon note
end if

Edits: fixed omitted dot in reported missing file names; numbers now stored and compared as integers rather than as text.

I apologize but my signature is clear :
Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) jeudi 3 mars 2016 11:25:33

set wrongName to "tb0020_comp_v028.999.dpx"
set higher to (text -8 thru -5) of wrongName
log result (*.999*)
set higher to higher as integer
log result (*.999*)

set wrongName to "tb0020_comp_v028,999.dpx"
set higher to (text -8 thru -5) of wrongName
log result (*,999*)
set higher to higher as integer
log result (*1*)

I’m a bit tired to read your comments like «it doesn’t work», «it’s impossible» when I pass the code AND the results obtained after running the code.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) jeudi 3 mars 2016 16:53:29

Your region difference affecting the outcome of a rounding operation is not something that even entered into my thought processes. It obviously didn’t enter into yours either, as my location is also posted, yet you declared that I was only dreaming there being a problem and went into voluminous detail about my perceived failures to read; you even went so far as to declare a perfectly valid 3-digit value as “wrongName.” :lol: There is no need to get snippy. :slight_smile:

Hi gents.

Could we please restrict on-forum squabbles to friendly discussions about code? Tetchiness and personal attacks spoil the friendly atmosphere, waste other people’s time and bandwidth, and do nothing to help OPs.

Ditto Nigel’s comments about wandering into tetchiness and personal comments. One snippy comment I’ll let pass but if you get into a give and go, I’ll delete them. One of the great features of this forum is its normally civil decorum.

Hi All,

I just wanted to say thank you so much for posting these different solutions for me to try! I did get confused by some errors I was getting at the beginning, because I’m new at this, but I really really appreciate it.

I got super busy at work yesterday and today so I haven’t been able to test out the recent suggestions but I will do so very soon.

Thank you all again!

Liat

I’ve been quite surprised to find that my second script (System Events for file names/vanilla for everything else) is actually slightly faster than my earlier one (shell script for names, filtering, sorting, and number extraction/vanilla for checking and reporting).

I’ve just edited it (post #22) so that the numbers are now stored and compared as integers rather than as text. It doesn’t make much difference when there are only 151 files, but it does slightly improve the performance as the number of files increases.

Nigel, thank you so much for your posts!

First of all, you are right, all the different codes were getting confusing for me and they were hard to troubleshoot because I am a beginner. I really appreciate you being aware that not everyone can understand the details of these discussions!

Second, I tried your script (post #22) and it worked perfectly! I tested it by removing one of the dpx files in my folder and it caught it right away. The script works very fast and was exactly what I had hoped for.

THANK YOU!!!

The only thing that would make this script even more powerful for me would be to make it somehow that I could select multiple folders to run the script on.

Usually I deliver 50-100 folders at a time and I have to make sure none of them has dropped frames (a semi-common occurrence) before I can deliver them. Even if the script stopped at the first dropped frame it caught, I would still be able to check multiple folders at once. IF you are in the mood to tweak the beautiful script you made me, that would be an amazing addition. If not, I totally understand and appreciate your help.

Thank you again.

Also, if there is any way I can copy the missing frame information to the clipboard, if any are missing, that would be so cool.

:slight_smile:

Liat

Hi Liat.

Would the multiple folders be all in the same place and all contain relevant subfolders called “2370x1800”?

Presumably, since you’re talking about the clipboard, you’d want one report covering the lot.

Hi Nigel,

Yes, I gather the relevant folders all in one place (in a delivery folder).
They all have the same folder structure as the example.

For instance:
DPXs_FOR_DELIVERY /
db0250_comp_v012 /
2370x1800 /
db0300_comp_v013 /
2370x1800 /
db0487_comp_v027 /
fd0060_comp_v014 /
fd0180_comp_v015 /
hd0130_comp_v018 /
mp0175_comp_v019 /
tb0280_comp_v029 /
tb0470_comp_v026 /

etc.

And yes, as far as copying the missing file information to the clipboard, the entire report would be great.

Thanks!
Liat

OK. This is basically the post #22 script with some extra code to apply it to multiple folders.

checkForMissingDPXs()

on checkForMissingDPXs()
	set theFolders to (choose folder with prompt "Select the _comp_ folders you want to check:" with multiple selections allowed)
	
	-- Get the missing names (if any) from each of the folders and append to the report.
	set report to ""
	repeat with thisFolder in theFolders
		set missingNames to checkFolder(thisFolder)
		if ((missingNames ends with ".dpx") or (missingNames ends with "!")) then set report to report & (linefeed & missingNames)
	end repeat
	
	if (report is "") then
		display dialog "No broken name sequences found." buttons {"OK"} default button 1 with icon note
	else
		-- The report text begins with a linefeed, which isn't wanted in the clipboard version.
		set the clipboard to text 2 thru -1 of report
		display alert "Broken Frames Detected!" message ("This has been copied to the clipboard:" & report) buttons {"OK"} default button 1
	end if
end checkForMissingDPXs

-- The checking part of the original script, rearranged so that error conditions don't stop the script.
on checkFolder(theFolder)
	set theNumbers to {} -- A collector for the serial numbers from the names
	set astid to AppleScript's text item delimiters
	
	try
		-- Just tell System Events to return all the file names in the folder. The script itself can filter for extensions much faster.
		tell application "System Events" to set theNames to name of every file of folder "2370x1800" of theFolder
		
		set firstHit to true -- "First hit" flag. Certain variables will be initialised on the first ".dpx" hit, updated thereafter.
		set AppleScript's text item delimiters to "."
		
		-- There's no need to sort the names. The lowest and highest numbers can be determined as they're extracted.
		repeat with i from 1 to (count theNames)
			set thisName to item i of theNames
			if (thisName ends with ".dpx") then
				-- If this name ends with ".dpx", extract and store the serial number from it.
				set thisNumber to (text item 2 of thisName) as integer
				set end of theNumbers to thisNumber
				if (firstHit) then
					-- On the first ".dpx" hit, get the base name and initialise the "highest" and "lowest" number variables.
					set baseName to (text item 1 of thisName) & "."
					set highestNumber to thisNumber
					set lowestNumber to thisNumber
					set firstHit to false
				else
					-- On subsequent hits, update the highest or lowest number.
					if (thisNumber > highestNumber) then
						set highestNumber to thisNumber
					else if (thisNumber < lowestNumber) then
						set lowestNumber to thisNumber
					end if
				end if
			end if
		end repeat
	on error errMsg
		display dialog errMsg buttons {"OK"} default button 1 with icon note
	end try
	set AppleScript's text item delimiters to astid
	
	if (theNumbers is {}) then
		tell application "System Events" to set theFolder to theFolder's name
		set missingNames to "No .dpx files in folder " & theFolder & "!"
	else
		set missingNames to "Files missing:"
		repeat with thisNumber from (lowestNumber + 1) to (highestNumber - 1)
			if (thisNumber is not in theNumbers) then
				set expectedName to baseName & thisNumber & ".dpx"
				set missingNames to missingNames & linefeed & expectedName
			end if
		end repeat
	end if
	
	return missingNames
end checkFolder

You may want to adjust this to your own requirements. As it’s written, you use the ‘choose folder’ dialog to navigate to the DPXs_FOR_DELIVERY folder and select the comp folder(s) you want to check.

If you’ll always want to check all of them, you could replace .

set theFolders to (choose folder with prompt "Select the _comp_ folders you want to check:" with multiple selections allowed)

. with .

set deliveryFolder to (choose folder with prompt "Select the DPXs_FOR_DELIVERY folder:")
tell application "System Events" to set theFolders to folders of deliveryFolder

. and select the DPXs_FOR_DELIVERY folder instead. (Don’t be tempted to write tell application “System Events” to set theFolders to folders of (choose folder with prompt “Select the DPXs_FOR_DELIVERY folder:”) as this will bring System Events to the front to display the dialog, which will cause problems with the other dialog(s).)

If you’ll always want to check all the folders and the DPXs_FOR_DELIVERY folder will alway be on your desktop, you could dispense with the ‘choose’ dialog altogether by using this instead:

tell application "System Events" to set theFolders to folders of folder "DPXs_FOR_DELIVERY" of desktop folder

Let me know if there are any problems.

Edit: bug fix.

This is incredible! It worked perfectly! Thank you so, so, so much!

You have just helped me make my work that much more efficient and my results more reliable.

I ended up replacing the Select Folder part with the script where I just choose the delivery folder, wherever it happens to be.

Thank you a million times.

Hi Liat.

Glad the script does what you want. :slight_smile:

I’ve just fixed a minor bug in the way the message is produced when there are no .dpx files in a folder. I’ve now inserted a line (after ‘if (theNumbers is {}) then’, near the bottom) telling System Events to get the name of the comp folder. It’s this which is now inserted into the message in the following line. Previously, the message-building line depended on the automatic coercion of theFolder to a text path. But this only worked while theFolder was an alias. If it’s a System Events reference ” as it is if you’re using the version of the code which selects the delivery folder ” the attempted coercion would cause an error. The new arrangement works with both System Events references and aliases.

Thank you, Nigel!

I’m excited to try this update, but I’m having trouble finding exactly which script is the new updated one with the bug fixed.

Can you point me to where it is?

Thanks,
Liat

Hi Liat.

It’s still the script in post #33, which I edited after discovering the bug. It now has an additional line immediately after ‘if (theNumbers is {}) then’:

if (theNumbers is {}) then
	tell application "System Events" to set theFolder to theFolder's name -- Additional line.
	set missingNames to "No .dpx files in folder " & theFolder & "!"

This short section checks the list of serial numbers collected from a folder and, if the list is empty, creates an entry for the log saying there are no .dpx files in the folder (although more accurately they’re not in the “2370x1800” subfolder). It’s not a situation that’s likely to occur, but the catch is there in case it does.

The way I originally wrote the script, the value of theFolder was an alias returned by the ‘choose folder’ dialog, eg.:

This could be inserted directly into the missingNames text and be automatically coerced to text itself, making the path to the folder appear in the text. But if you use one of my suggested modifications ” as you say you have ” theFolder will be set as a System Events reference to the folder:

This works everywhere else in the script, but can’t be coerced to text. So the fix tells System Events to extract the name of theFolder ” which it can do with either an alias or one of its own references ” and inserts that into the text instead. The name’s probably preferable to the full path here anyway, but you could use ‘path’ instead of ‘name’ in the added line if you wanted that.

Hi Nigel,

You are a genius.

  1. The bug you fixed is really helpful because it did indeed catch folders where there were no frames at all - about 52 out of 1400 folders. I caught them when I ran the script initially and I had to click “OK” for each error message!

  2. Thank you for EXPLAINING what you did and how it works.

  3. :D:D:D:D :smiley: Thank you.

Ouch! :rolleyes: