Renaming files to Odd or Even numbers.

I was wondering if someone could help me with writing a script that renames files in a folder to just odd numbers. ex.
0001.jpg
0003.jpg
0005.jpg

I would also need a seperate one for evens numbers. I would like not to have to deal with any dialog boxes if possible.

Any help would be great!

Hi,

try this


property even : true -- set to false to get odd numbers

set thefolder to choose folder

set TheNumber to (even as integer) + 1
tell application "Finder"
	set FileList to every file of thefolder
	repeat with oneFile in FileList
		set Ex to name extension of oneFile
		set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
		set name of oneFile to NameString
		set TheNumber to TheNumber + 2
	end repeat
end tell


Thank you so much. I was wondering if this could be changed a bit though. I don’t want to have to choose the folder with a dialog box instead I would rather set the path in the script since it will always be pointed to same folder. Thanks for helping me out on this.

London

Hi,

To set the path you would change the following line in Stefan’s code.


set theFolder to choose folder
--to
set theFolder to "path:to:folder:"

If you are unsure of what the path is then select the folder in the Finder
and run this script. The path will be on your clipboard.

tell application "Finder"
	try
		set theSelection to a reference to the selection
	on error
		display dialog "You must have something selected." buttons {"OK"} default button 1
		error number -128
	end try
	set thePath to theSelection as alias
	set the clipboard to "\"" & thePath & "\""
end tell

Cheers,

Craig

Okay thats what I thought. But when I change it I get this Apple Script Error: Can’t get every file of “London:Users:london:Desktop:Flash:Front:”. This is how I did it.


property even : false -- set to false to get odd numbers

set thefolder to "London:Users:london:Desktop:Flash:Front:"

set TheNumber to (even as integer) + 1
tell application "Finder"
	set FileList to every file of thefolder
	repeat with oneFile in FileList
		set Ex to name extension of oneFile
		set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
		set name of oneFile to NameString
		set TheNumber to TheNumber + 2
	end repeat
end tell

Change this line to:

set thefolder to "London:Users:london:Desktop:Flash:Front:" as alias

Thanks so much … just curious what does as alias do?

Since theFolder path was not defined inside a Finder or System Events tell block
we need to inform the system that this is a path and not just a string.

Another way to specify a path to the system.
We will not have to specify an alias
but inform the Finder that the string path is
actually a folder.


property even : false -- set to false to get odd numbers

set TheNumber to (even as integer) + 1
tell application "Finder"
	set thefolder to folder "G5:Users:craig:Desktop:DSC1:"
	set FileList to every file of thefolder
	repeat with oneFile in FileList
		set Ex to name extension of oneFile
		set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
		set name of oneFile to NameString
		set TheNumber to TheNumber + 2
	end repeat
end tell

hth,

Craig

PS.

Okay I want to take this script a little further. I would like this script to number the files in reverse order if this is possible. So if I had a total of 50 files it would name them:
0100.jpg
0098.jpg
0096.jpg
0094.jpg
ect…

Thanks Again for all the help.

London

property even : false -- set to false to get odd numbers

set TheNumber to (even as integer) + 1
tell application "Finder"
   set thefolder to folder "G5:Users:craig:Desktop:DSC1:"
   set FileList to every file of thefolder
   repeat with oneFile in FileList
       set Ex to name extension of oneFile
       set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
       set name of oneFile to NameString
       set TheNumber to TheNumber + 2
   end repeat
end tell

Try this, it considers all options


property even : false -- set to false to get odd numbers
property ascending : false -- set to false for descending
property startNumber : 100

if ascending then
	set TheNumber to (even as integer) + 1
else
	set TheNumber to startNumber - ((not even) as integer)
end if
tell application "Finder"
	-- if the folder is on desktop, you can refer to it just by the name
	set thefolder to folder "DSC1:" -- "G5:Users:craig:Desktop:DSC1:"
	set FileList to every file of thefolder
	set fileCount to count FileList
	if not ascending and startNumber ≤ (fileCount / 2) then
		display dialog "the number of files is less than the startNumber" & return & "Please choose a higher startNumber"
		return
	end if
	repeat with oneFile in FileList
		set Ex to name extension of oneFile
		set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
		set name of oneFile to NameString
		if ascending then
			set TheNumber to TheNumber + 2
		else
			set TheNumber to TheNumber - 2
		end if
	end repeat
end tell

Thanks for the help, however, I would like the script to determine how many files are in the folder and determine the countdown number automatically. Also I need the script to work in automator when I place this script into automator I get a Syntax Error: Expected “end” but found “property”. This is how I have it in automator.

on run {input, parameters}
	
	property even : false -- set to false to get odd numbers
property ascending : false -- set to false for descending
property startNumber : 9999
if ascending then
	set TheNumber to (even as integer) + 1
else
	set TheNumber to startNumber - ((not even) as integer)
end if
tell application "Finder"
	-- if the folder is on desktop, you can refer to it just by the name
	set thefolder to folder "London:Users:london:Documents:iPod Flash Cards:Temp:" -- "G5:Users:craig:Desktop:DSC1:"
	set FileList to every file of thefolder
	set fileCount to count FileList
	if not ascending and startNumber ≤ (fileCount / 2) then
		display dialog "the number of files is less than the startNumber" & return & "Please choose a higher startNumber"
		return
	end if
	repeat with oneFile in FileList
		set Ex to name extension of oneFile
		set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
		set name of oneFile to NameString
		if ascending then
			set TheNumber to TheNumber + 2
		else
			set TheNumber to TheNumber - 2
		end if
	end repeat
end tell
	return input
end run

defining a property in an explizit run handler is not allowed.
You can use this instead


on run {input, parameters}
	
	set even to false -- set to false to get odd numbers
	set ascending to false -- set to false for descending
	set startNumber to 9999
.

PS:

I haven’t tested it, but try this


on run {input, parameters}
	
	set even to false -- set to false to get odd numbers
	set ascending to false -- set to false for descending
	tell application "Finder"
		-- if the folder is on desktop, you can refer to it just by the name
		set thefolder to folder ((path to documents folder as text) & "iPod Flash Cards:Temp:")
		set FileList to every file of thefolder
		set fileCount to count FileList
		set startNumber to (count FileList * 2)
		
		if ascending then
			set TheNumber to (even as integer) + 1
		else
			set TheNumber to startNumber - ((not even) as integer)
		end if
		
		repeat with oneFile in FileList
			set Ex to name extension of oneFile
			set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
			set name of oneFile to NameString
			if ascending then
				set TheNumber to TheNumber + 2
			else
				set TheNumber to TheNumber - 2
			end if
		end repeat
	end tell
	return input
end run

The last script did not work but I figured out what I needed myself. This script will name files with odd or even numbers in descending order.

on run {input, parameters}
	
	set targFol to "London:Users:london:Documents:iPod Flash Cards:Temp:"
	
	(* set endCharacters to ".jpeg"*)
	-- remove (*,*) if you want it to count only files with certain name or ext. Also remove (*,*) the ones below too.
	
	tell application "Finder"
		set myList to every file of folder targFol (* whose name ends with endCharacters *)
		-- remove (*,*) to activate endCharacters
		
		
		set startNumber to count of myList
		
		set even to true -- set to false to get odd numbers
		set ascending to false -- set to false for descending
		set startNumber to startNumber * 2
		
		if ascending then
			set TheNumber to (even as integer) + 1
		else
			set TheNumber to startNumber - ((not even) as integer)
		end if
		tell application "Finder"
			-- if the folder is on desktop, you can refer to it just by the name
			set thefolder to folder "London:Users:london:Documents:iPod Flash Cards:Temp:" -- "G5:Users:craig:Desktop:DSC1:"
			set FileList to every file of thefolder
			set fileCount to count FileList
			if not ascending and startNumber ≤ (fileCount / 2) then
				display dialog "the number of files is less than the startNumber" & return & "Please choose a higher startNumber"
				return
			end if
			repeat with oneFile in FileList
				set Ex to name extension of oneFile
				set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
				set name of oneFile to NameString
				if ascending then
					set TheNumber to TheNumber + 2
				else
					set TheNumber to TheNumber - 2
				end if
			end repeat
		end tell
	end tell
	
	return input
end run

Thanks everyone for helping me with this script it makes my life a lot easier.

Sorry, this script can’t work, because you mixed up the scripts and some of the parts are twice.

Try exactly this script, the path to the folder is also correct using this syntax


on run {input, parameters}
	
	set even to false -- set to false to get odd numbers
	set _ascending to false -- set to false for descending
	tell application "Finder"
		-- if the folder is on desktop, you can refer to it just by the name
		set thefolder to folder ((path to documents folder as text) & "iPod Flash Cards:Temp:")
		set FileList to every file of thefolder
		set fileCount to count FileList
		set startNumber to (fileCount * 2)
		
		if _ascending then
			set TheNumber to (even as integer) + 1
		else
			set TheNumber to startNumber - ((not even) as integer)
		end if
		
		repeat with oneFile in FileList
			set Ex to name extension of oneFile
			set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
			set name of oneFile to NameString
			if _ascending then
				set TheNumber to TheNumber + 2
			else
				set TheNumber to TheNumber - 2
			end if
		end repeat
	end tell
	return input
end run

Thanks so much I apprieciate the help. Just to let you know that script does work but since I have very little clue about applescript I am sure somethings I could have said once are probably being said twice for no good reason. :wink: Thanks again. Now I can study my flash cards on my iPod classic for school!!!

Interestingly, if I have file names in the following manner: file 1, file 2, …file 10, file 11, file 12, the renaming to odd or even, considers file10 to be next in order after file 1. How can this be fixed?

Obviously, numerically, file 2 comes after file 1, not file 10.

sort the file list explicitly be name


.
set FileList to sort every file of theFolder by name
.

Nice to see your continued involvement in the AS forum Stefan! Applescript is certainly not dead!

This worked perfectly BTW.

Marlon