Renaming files by changing the first four character

Before posting this topic I did a search on the forum using “Renaming files” and I have been unable to find the correct answer to my question.

Here is the situation

I’ve got a folder called “Macintosh HD:Users:DPaquin:_RenamingFileFolder” which contains mp4 files. For each of the mp4 files contained in that folder I need to change the first four characters (M2U0) to YTB0.

For examples :

  • M2U04707.mp4 —> YTB04707.mp4
  • M2U05502.mp4 —> YTB05502.mp4
  • M2U03107.mp4 —> YTB03107.mp4

At the moment I am getting the following error message error “Can’t get every file of "Macintosh HD:Users:DPaquin:_RenamingFileFolder:".” number -1728 from every file of “Macintosh HD:Users:DPaquin:_RenamingFileFolder:” on the Tell application “Finder” call.

I cannot see what the error code -1728 is. However, I do no know the folder _RenamingFileFolder does exist.

As per the rest, I wrote the following code. I will try to find an answer to my error message. However, does anyone believe my code will be working?

THANKS AGAIN for your help!
Daniel

set RenameFilesWithinFolder to (path to home folder as text) & "_RenamingFolder:"

tell application "Finder" to set FilesToBeRenamed to files of RenameFilesWithinFolder whose name extension is "mp4"

repeat with i from 1 to count FilesToBeRenamed
	
	set ValidPartName to characters 5 thru 12 of item i
	set ValidName to "YTB0" & ValidPartName
	tell application "Finder" to set name of item i to ValidName
	
end repeat
tell application "Finder" to set FilesToBeRenamed to files of folder RenameFilesWithinFolder whose name extension is "mp4"

a piece of text has no files :slight_smile:

Thanks mouramartins,

While you were providing me with the answer to my first problem I was writing that I had fixed my problem. I did indeed, as you recommended added the words “of folder” after files. It now works and I no longer have the error code -1728.

tell application "Finder" to set FilesToBeRenamed to files of folder RenameFilesWithinFolder whose name extension is "mp4"

However, I now get the error message error “Invalid range.” number -1720 from characters 5 thru 12 of item 1

Try:

set RenameFilesWithinFolder to (path to home folder as text) & "_RenamingFileFolder"

tell application "System Events"
	set FilesToBeRenamed to every file of folder RenameFilesWithinFolder whose name extension is "mp4"
	repeat with aFile in FilesToBeRenamed
		set ValidPartName to text 5 thru -1 of (get aFile's name)
		set aFile's name to "YTB0" & ValidPartName
	end repeat
end tell

Many thanks adayzdone,

It does exactly what I wanted.

Great.

Daniel