Checking that first 6 characters of file name are numbers

Here’s my problem, i got an automated script that organize files using the first 6 characters of the filename. It creates subfolders and put files where they belong. I want to check that the first 6 characters are numbers before they are moved in the subfolder.
i suppose i have to use a property with numbers in it, and a repeat function but i don’t know how.
here’s the actual code:


property extension_list : {"eps", "gif"}
property extension_quality : {"_I_BDP.gif", "_S_BDP.gif", "_S_BDM.gif", "_S_BDG.gif", "_I_BDM.gif", "_I_BDG.gif", "_I_HD.eps", "_S_HD.eps"}
property onlyNumber : {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
on adding folder items to DossierParent after receiving ListeFichiers
	tell application "Finder"
		repeat with Boucle from 1 to number of items in ListeFichiers
			set LeFichier to item Boucle of ListeFichiers
			
			set NomFichier to name of LeFichier
			set L_extension to name extension of LeFichier
			set cheminPAO to ("PAO" & ":IMAGES:")
			set Nomproduit to (text 1 through 6 of NomFichier)
			set NomDossier1 to (text 1 through 2 of NomFichier)
			set NomDossier2 to (text 3 through 4 of NomFichier)
			set NomDossier3 to (text 5 through 6 of NomFichier)
			
			
			set CheminNiveau1 to (((DossierParent) as string) & NomDossier1 & ":")
			set CheminNiveau2 to (CheminNiveau1 & NomDossier2 & ":")
			set CheminNiveau3 to (CheminNiveau2 & NomDossier3 & ":")
			
			--here is my problem: this way i have to check each character from 1to 6
repeat with Lettre in text 1 through end of nomproduit
if Lettre is in onlyNumber then
				
--HUMM. 

try
					if L_extension is in extension_list then
						try
							if ("_I.eps" is in NomFichier) or ("_S.eps" is in NomFichier) then
								set name of LeFichier to ((text -5 through beginning of NomFichier) & "_HD." & (L_extension as string))
							end if
						end try
						set nouveaunom to name of LeFichier
						try
							if (text 9 through end of nouveaunom) is in extension_quality then
								try
									duplicate LeFichier to cheminPAO as alias with replacing
								on error
									try
										move LeFichier to cheminPAO as alias with replacing
									on error
										display dialog "V�rifier le d�placement du fichier :" & NomFichier & " sur PAO"
									end try
									
								end try
								try
									move LeFichier to CheminNiveau3 as alias with replacing
								on error
									try
										set Dossier1 to CheminNiveau1 as alias
									on error
										set Dossier1 to (make new folder at DossierParent with properties {name:NomDossier1}) as alias
									end try
									
									try
										set Dossier2 to CheminNiveau2 as alias
									on error
										set Dossier2 to (make new folder at Dossier1 with properties {name:NomDossier2}) as alias
									end try
									
									try
										set Dossier3 to (make new folder at Dossier2 with properties {name:NomDossier3}) as alias
									end try
									try
										move LeFichier to Dossier3 with replacing
									end try
								end try
							else
								display dialog "V�rifier le nommage de ce fichier : " & NomFichier & ", il ne sera pas archiv�." buttons {"OK"} default button 1 with icon 0
								
								--display dialog "alors A  : " & (text -5 through -6 of NomFichier as string) & " et " & text 7 of NomFichier
							end if
						end try
					else
						display dialog "le fichier :" & NomFichier & " est un fichier �" & (L_extension as string) & "�, il ne sera pas archiv�. " buttons {"OK"} default button 1 with icon 0
					end if
				end try
				--				
			else
				
				display dialog "V�rifier le nommage de ce fichier : " & NomFichier & ", il ne sera pas archiv�." buttons {"OK"} default button 1 with icon 0
				
			end if
		end repeat
	end tell
end adding folder items to

Add the following subroutine to the end of your script (outside the folder action handler).

on is_num(string_, length_to_check)
	try
		set test_ to (characters 1 thru length_to_check of string_) as list
		repeat with i in test_
			set in_numbers to i is in onlyNumber
			if in_numbers is false then return false
		end repeat
		return true
	on error
		return false
	end try
end is_num

Then, where you started building the code to repeat through the characters, replace it with the following and adjust it to suit your workflow.

if my is_num(nomproduit, 6) is true then
	-- the string is a number
	-- continue script
else
	-- the string is not a number
	-- do something else
end if

– Rob


set text01 to "abcdef <-- not a number"
set text02 to "123456 <--     a number"

set num01 to text01's text 1 thru 6 --> "abcdef"
set num02 to text02's text 1 thru 6 --> "123456"

try
	set num01 to num01 as number --> error 'Can't make "abcdef" into a number.'

on error
	--> handler the error
end try

try
	set num02 to num02 as number --> 123456

on error
	--> handler the error
end try

Hi Arthur,

Just an FYI…

I was going to suggest something similar to your solution but certain characters (+ and -) will pass the test as a number even though they are not acceptable in the final result. For instance:

try
	set foo to "+++++" as number
end try
--> 0

– Rob

Good point. :slight_smile:

Oddly enough, I didn’t see your response when I posted mine. I think we must have hit the submit button at around the same moment in time. :slight_smile:

I wonder how a more hard-coded solution might do, speed-wise, for a whole lot of names to test:

set sixChars to "314159"
set numChars to "0123456789"

considering case -- is faster for string comparisons
	
	if (sixChars's character 1 is in sNumbers) and ¬
		(sixChars's character 2 is in sNumbers) and ¬
		(sixChars's character 3 is in sNumbers) and ¬
		(sixChars's character 4 is in sNumbers) and ¬
		(sixChars's character 5 is in sNumbers) and ¬
		(sixChars's character 6 is in sNumbers) then

		-- whatever

	else

		-- whatever

	end if
end considering

Yup, I think so. :slight_smile:

A quick test indicates that your hard-coded solution is much quicker. It just needs a quick fix for a typo.

set sixChars to "314159" 
 set sNumbers to "0123456789" 
  
 considering case -- is faster for string comparisons 
     
    if (sixChars's character 1 is in sNumbers) and ¬ 
       (sixChars's character 2 is in sNumbers) and ¬ 
       (sixChars's character 3 is in sNumbers) and ¬ 
       (sixChars's character 4 is in sNumbers) and ¬ 
       (sixChars's character 5 is in sNumbers) and ¬ 
       (sixChars's character 6 is in sNumbers) then 
  
       -- whatever 
  
    else 
  
       -- whatever 
  
    end if 
 end considering

Cheers!
Rob

Guys, it’s wonderful. It works smoothly. Now I have to look more precisely what’s this wonderful “consider” thing. One more time you helped me make my life simplier. hurra !
I was wondering, is there a more elegant way to check the path before moving the file to its folder. I’m talking about this code :

try
										move LeFichier to CheminNiveau3 as alias with replacing
									on error
										try
											set Dossier1 to CheminNiveau1 as alias
										on error
											set Dossier1 to (make new folder at DossierParent with properties {name:NomDossier1}) as alias
										end try
										
										try
											set Dossier2 to CheminNiveau2 as alias
										on error
											set Dossier2 to (make new folder at Dossier1 with properties {name:NomDossier2}) as alias
										end try
										
										try
											set Dossier3 to (make new folder at Dossier2 with properties {name:NomDossier3}) as alias
										end try
										try
											move LeFichier to Dossier3 with replacing
										end try
									end try
 

I mean, i’m not sure that using an error is the more efficient to do it.
Anyway, having a better script might not be such a good idea. this is a good script (it works) and this is what i’m expecting from it.

Here’s the code of the actual working version:

property TIRETok : {"-"}
property extension_list : {"eps", "gif"}
property extension_quality : {"_I_BDP.gif", "_S_BDP.gif", "_S_BDM.gif", "_S_BDG.gif", "_I_BDM.gif", "_I_BDG.gif", "_I_HD.eps", "_S_HD.eps"}
property sNumbers : {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
on adding folder items to DossierParent after receiving ListeFichiers
	tell application "Finder"
		repeat with Boucle from 1 to number of items in ListeFichiers
			set LeFichier to item Boucle of ListeFichiers
			
			set NomFichier to name of LeFichier
			set Nomproduit to (text 1 through 6 of NomFichier)
			
			considering case -- is faster for string comparisons 
				
				if (Nomproduit's character 1 is in sNumbers) and ¬
					(Nomproduit's character 2 is in sNumbers) and ¬
					(Nomproduit's character 3 is in sNumbers) and ¬
					(Nomproduit's character 4 is in sNumbers) and ¬
					(Nomproduit's character 5 is in sNumbers) and ¬
					(Nomproduit's character 6 is in sNumbers) then
					
					
					set L_extension to name extension of LeFichier
					--set cheminPAO to ("PAO" & ":IMAGES:")
					
					set NomDossier1 to (text 1 through 2 of NomFichier)
					set NomDossier2 to (text 3 through 4 of NomFichier)
					set NomDossier3 to (text 5 through 6 of NomFichier)
					
					
					set CheminNiveau1 to (((DossierParent) as string) & NomDossier1 & ":")
					set CheminNiveau2 to (CheminNiveau1 & NomDossier2 & ":")
					set CheminNiveau3 to (CheminNiveau2 & NomDossier3 & ":")
					
					try
						if L_extension is in extension_list then
							try
								if ("_I.eps" is in NomFichier) or ("_S.eps" is in NomFichier) then
									set name of LeFichier to ((text -5 through beginning of NomFichier) & "_HD." & (L_extension as string))
								end if
							end try
							set nouveaunom to name of LeFichier
							try
								if (text 9 through end of nouveaunom) is in extension_quality then
									try
										duplicate LeFichier to cheminPAO as alias with replacing
									on error
										try
											move LeFichier to cheminPAO as alias with replacing
										on error
											display dialog "Vérifier le déplacement du fichier :" & NomFichier & " sur PAO"
										end try
										
									end try
									try
										move LeFichier to CheminNiveau3 as alias with replacing
									on error
										try
											set Dossier1 to CheminNiveau1 as alias
										on error
											set Dossier1 to (make new folder at DossierParent with properties {name:NomDossier1}) as alias
										end try
										
										try
											set Dossier2 to CheminNiveau2 as alias
										on error
											set Dossier2 to (make new folder at Dossier1 with properties {name:NomDossier2}) as alias
										end try
										
										try
											set Dossier3 to (make new folder at Dossier2 with properties {name:NomDossier3}) as alias
										end try
										try
											move LeFichier to Dossier3 with replacing
										end try
									end try
								else
									display dialog "Vérifier le nommage de ce fichier : " & NomFichier & ", il ne sera pas archivé." buttons {"OK"} default button 1 with icon 0
									
									--display dialog "alors A  : " & (text -5 through -6 of NomFichier as string) & " et " & text 7 of NomFichier
								end if
							end try
						else
							display dialog "le fichier :" & NomFichier & " est un fichier “" & (L_extension as string) & "”, il ne sera pas archivé. " buttons {"OK"} default button 1 with icon 0
						end if
					end try
					--				
				else
					
					display dialog "Vérifier le code produit de ce fichier : " & NomFichier & ", il ne sera pas archivé." buttons {"OK"} default button 1 with icon 0
					
				end if
			end considering	
		end repeat
	end tell
end adding folder items to

If you are looking for more “elegant”, then the Finder’s “exists” command is more readable, perhaps. However, what you’re doing is the most efficient: error-trapping an “as alias” coercion. The only thing I would recoomend is that you throw it into a handler to keep your code’s logic easier to read:


set Dossier1 to EnsureFolderExists(DossierParent, NomDossier1)
set Dossier2 to EnsureFolderExists(Dossier1, NomDossier2)
set Dossier3 to EnsureFolderExists(Dossier2, NomDossier3)

tell application "Finder"
	
	move LeFichier to Dossier3 with replacing
	
end tell

-- Create a folder if it does not exist.
--
on EnsureFolderExists(folderLocation, folderName)
	try
		return alias ((folderLocation as string) & folderName)
	on error
		tell application "Finder"
			make new folder at folderLocation with properties {name:folderName}
			return alias ((folderLocation as string) & folderName)
		end tell
	end try
end EnsureFolderExists

Thanks again for your advices and comments.