Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Remove last 5 digits of filename and add sequence number

I need to rename files as follows:


Existing file name structure is:

UWSTD-NV-16999.jpg

UWSTD-NV-17001.jpg

UWSTD-NV-17006.jpg


I would like to rename like the following:

UWSTD-NV-1.jpg

UWSTD-NV-2.jpg

UWSTD-NV-3.jpg


I have a script that will remove the numbers only from the end, but I'm not sure how I can modify it to add a sequence number on the end.


on run {input, parameters}

repeat with thisFile in input

tell application "Finder"

set {theName, theExtension} to {name, name extension} of thisFile

if theExtension is in {missing value, ""} then

set theExtension to ""

else

set theExtension to "." & theExtension

end if

set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part

set theName to (do shell script "echo " & quoted form of theName & " | sed 's/[0-9]*$//'") -- strip trailing numbers

set name of thisFile to theName & theExtension

end tell

end repeat

return input

end run

macOS Sierra (10.12.4)

Posted on Mar 5, 2018 6:44 AM

Reply
Question marked as Best reply

Posted on Mar 5, 2018 6:49 AM

Use code such as:


on run {input, parameters}

set theNumber to 1

repeat with thisFile in input

tell application "Finder"

set {theName, theExtension} to {name, name extension} of thisFile

if theExtension is in {missing value, ""} then

set theExtension to ""

else

set theExtension to "." & theExtension

end if

set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part

set theName to (do shell script "echo " & quoted form of theName & " | sed 's/[0-9]*$//'") -- strip trailing numbers

set name of thisFile to theName & " " & theNumber & " " & theExtension

set theNumber to theNumber + 1

end tell

end repeat

return input

end run


(158401)

Similar questions

5 replies
Question marked as Best reply

Mar 5, 2018 6:49 AM in response to krispypulos

Use code such as:


on run {input, parameters}

set theNumber to 1

repeat with thisFile in input

tell application "Finder"

set {theName, theExtension} to {name, name extension} of thisFile

if theExtension is in {missing value, ""} then

set theExtension to ""

else

set theExtension to "." & theExtension

end if

set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part

set theName to (do shell script "echo " & quoted form of theName & " | sed 's/[0-9]*$//'") -- strip trailing numbers

set name of thisFile to theName & " " & theNumber & " " & theExtension

set theNumber to theNumber + 1

end tell

end repeat

return input

end run


(158401)

Mar 5, 2018 7:40 AM in response to Niel

Thanks Niel!


It works, but now I realize I want it to only rename files with similar names sequentially and then restart the count when there is a unique filename.


Old names:

BWCHPT-PR-16964.jpg

BWCHPT-PR-16966.jpg

BWCHPT-PR-16974.jpg

BWCHRT-PK-16932.jpg

BWCHRT-PK-16936.jpg

BWCHRT-PK-16939.jpg

BWCHRT-WH-16873.jpg

BWCHRT-WH-16878.jpg

BWCHRT-WH-16881.jpg


New names:

BWCHPT-PR-3.jpg

BWCHPT-PR-4.jpg

BWCHPT-PR-5.jpg

BWCHRT-PK-3.jpg

BWCHRT-PK-4.jpg

BWCHRT-PK-5.jpg

BWCHRT-WH-3.jpg

BWCHRT-WH-4.jpg

BWCHRT-WH-5.jpg

Mar 5, 2018 7:45 AM in response to krispypulos

Try using:


on run {input, parameters}

repeat with thisFile in input

tell application "Finder"

set {theName, theExtension} to {name, name extension} of thisFile

if theExtension is in {missing value, ""} then

set theExtension to ""

else

set theExtension to "." & theExtension

end if

set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part

set theName to (do shell script "echo " & quoted form of theName & " | sed 's/[0-9]*$//'") -- strip trailing numbers

try

set name of thisFile to theName & theExtension

on error

set theNumber to 1

repeat

try

set name of thisFile to theName & " " & theNumber & " " & theExtension

exit repeat

on error

set theNumber to theNumber + 1

end try

end repeat

end try

end tell

end repeat

return input

end run


(158404)

Remove last 5 digits of filename and add sequence number

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.