set theFolder to (choose folder with prompt “Please select the folder containing the files to rename.”)
set outFolder to (choose folder with prompt “Please select the folder to move renamed files to.”)
tell application “Finder”
--set aFile to first file of theFolder
repeat with aFile in theFolder
set theName to "" as string
set theName to name of aFile
set theName to theName as string
set keepThese to (characters 1 through 8 of theName)
set plonk to ""
set plonk to (characters 9 through end of theName)
set plonk to plonk as string
set x to length of theName
if x = 11 then
move aFile to outFolder
else if x is equal to 9 then
set theNewName to (keepThese & "00" & plonk as string)
set name of file (aFile as alias) to theNewName
move file (aFile as alias) to outFolder
else
set theNewName to (keepThese & "0" & plonk as string)
set name of file (aFile as alias) to theNewName
move file (aFile as alias) to outFolder
end if
end repeat
end tell
END CODE
When I select the same folder for both of the prompts this script works fine. But if I make the out folder a different folder, the script processes half the files in the folder and then errors out. It doesn’t matter if there are 10 or 100 files (all of the files start with the same name, only the number at the end is different), it processes half of them and then gives me an error. Am I doing something wrong with the move statement? Or some other statement?
If there are 4 things in your folder it will repeat in this fashion:
item 1 of alias HD:Desktop Folder:some folder:
item 2 of alias HD:Desktop Folder: some folder:
item 3 of alias HD:Desktop Folder: some folder:
item 4 of alias HD:Desktop Folder: some folder:
Now, remember, you are moving files out of there so when it gets to item 3, in this scenario, there is no item 3 of folder theFolder - you moved it.
Instead, and I have much better luck with this, get the name of every file of your folder
and repeat with every name. In each pass, piece the string that is the path to the folder together with the name of the file to get your file path. This reduces the direct calls to the Finder too so you won’t get “Out Of Memory” errors. Try the following:
set theFolder to (choose folder with prompt "Please select the folder containing the files to rename.")
set outFolder to (choose folder with prompt "Please select the folder to move renamed files to.")
--set aFile to first file of theFolder
(*the reason your script errors is the repeat statement. You are repeating with
every file in the folder but at the same time moving those files. This is why you didn't
get the same error when using the same folder. Instead get the name of every file in the folder
and repeat with the list of names instead of pointing it to aliases.*)
set theFolder to theFolder as string --coerce the folder datatype from alias to string
set outFolder to outFolder as string --coerce the out folder datatype from alias to string
tell application "Finder"
set nameList to get the name of every file of folder theFolder --faster than getting every file of
end tell
repeat with theName in nameList
set thisFile to theFolder & theName as string --concatenate the folder path and file name (faster than repeating with every alias)
set movedFile to outFolder & theName as string --concatenate the out folder path and file name (this is where the file will end up)
set keepThese to (characters 1 through 8 of theName)
set plonk to (characters 9 through end of theName) as string
set x to length of theName
if x = 11 then
tell application "Finder"
try
move file thisFile to folder outFolder --If that file name already exists here this will fail.
on error --name already exists
--do something with the duplicate file
end try
end tell
else if x = 9 then
set theNewName to (keepThese & "00" & plonk as string)
tell application "Finder"
move file thisFile to folder outFolder
try
set name of file movedFile to theNewName --If that file name already exists here this will fail.
on error --name already exists
--do something with the duplicate file
end try
end tell
else
set theNewName to (keepThese & "0" & plonk as string)
tell application "Finder"
move file thisFile to folder outFolder
try
set name of file movedFile to theNewName --If that file name already exists here this will fail.
on error --name already exists
--do something with the duplicate file
end try
end tell
end if
end repeat
I switched around the order of when it moves and renames the file and added error handling for the rename in case the name already exists. You’ll need to figure out what you want to do with a file if a file by the same name already exists.