Rename file by copying the first five (5) characters, deleting them and then paste at the end

I am really liking the “Quick Action” of Automator. So convenient and so efficient to use… I do have KM and Hazel, but I am hoping to channel all of my limited (Think Old) learning curve to one Program. For reasons beyond me, Automator makes me feel comfortable and willing to do the learning curve.

The files that I work with begin with the years such as "1932 - City.pdf"or “1892 - France.pdf”…etc…

I have used a program called “A Better Finder Rename 11” to rename “1932 - BetterLife” to “BetterLife - 1932” .

so if the year is different, I just adjust the year. Then select all the pdf’s with same year and run

I would like to create a Quick action with the same thought involved.

Can someone suggest a roadmap for me to use… what is difficult for me to understand, is how do I direct Automator to copy the first five characters , then delete the first five characters and then paste them at the end of the file

Shell scripting has been the de facto tool for file management since file systems were conceived. There’s almost always a way to do it from the command line as a result.

Based on that, my approach would be to use Automator to run a shell script against selected files to rename them.

Here’s how it looks.

2021-02-23-18.11.09

Here’s the script.

for f in "$@"
do
	new=$(echo $(basename "$f") | sed -E 's/^(.{5})(.*)\.(.*)/\2\1.\3/')
    mv "$f" "$(dirname "$f")/$new"
done

Here’s a link to the Automator file; though obviously please do confirm the content before running it, and always have a good backup in place.

The script receives the selected file paths from Finder. It splits out the base name from the directory path and then used a regular expression as an instruction to sed (stream editor) to rearrange the components of the file name. With the new name, the directory path is prefixed to it, and mv (move command) is used to rename the file. This is repeated in the do loop for each of the files received.

Hope that helps.

@sylumer

Although this is above my skill set, I was successful installing…

Original 1880 - A Hot Wave (Sheet)
After - A Hot Wave (Sheet)1880

What do I have to do to make the outcome
Original 1880 - A Hot Wave (Sheet)
Would Like A Hot Wave (Sheet) - 1880

I really like the explanations given…It is very helpful to understand why and what it is doing… I changed {5} to {6} and this put in a space (I know what that does now)

What do I need to do to change to make “- A Hot Wave (Sheet)1880” to “A Hot Wave (Sheet) - 1880”

I do appreciate the time and effort…

Just a learning opportunity.

I think I understand the question, but the lack of formatting and break up of content does not help, so I could be wrong on what you are asking for.

I think you want to rename

1880 - A Hot Wave (Sheet)

With an unknown file extension to

A Hot Wave (Sheet) - 1880

To do that you need to modify the regular expression. There are a few ways to do this, but something like this for the match and replace should work I think (not at my Mac right now).

/^(.*) - (.*)\.(.*)/\2 - \1.\3/

This is based off splitting by “-” rather than character count, but from this example and the one above you should be able to see how to do it that way.

For this one, again a different regular expression, but I’m really just making wild guesses at this point of what your criteria are for splitting this up. It could be by particular characters such as spaces and closing parentheses. It could be by numbers of characters. It could be by matching numeric characters. It could be a combination of all of them. But for completeness, I think this would cause the change as written.

/^- (.*)(.{4})\.(.*)/\1 - \2.\3/

@sylumer
I am away for a couple hours but I am anxious to return…

Original
1880[ ]-[ ] A[ ]Hot[ ]Wave 1880 - A Hot Wave (Sheet)

Would Like
A[ ]Hot[ ]Wave [ ]-[ ]1880 A Hot Wave (Sheet) - 1880

where [ ] is a space

Many thanks again
Ron from Canada

Is that not covered by first suggestion in my second post above?

Note also you seem to have a double space in your “original” entry above. It won’t change the answer, but I am guessing that is a typo.

This is the file that I would like to change the name from:
1880 - A Hot Wave (Sheet) to
A Hot Wave (Sheet) - 1880

Okay, so what did the above give you that I suggested, and are you now looking to change the date too (1880 → 1889)?

This is what I get when I update.

  • A Hot Wave (Sheet) 1880

Sorry about the 1889. should have been 1880. I have corrected

Are you sure you used the right expression?

This definitely looks to forcibly include a hyphen in the output.

Will be home in an hour.

Screen Shot 2021-02-23 at 3.54.16 PM

In the screenshot you have:

for f in "$@"
do
    new=$(echo $(basename "$f") | sed -E 's/^(.*) - (.*)\.(.*)/\2 - \1.\3/
    mv "$f" "$(dirname "$f")/$new"
done

but you should have

for f in "$@"
do
    new=$(echo $(basename "$f") | sed -E 's/^(.*) - (.*)\.(.*)/\2 - \1.\3/')
    mv "$f" "$(dirname "$f")/$new"
done

i.e. you have not closed the sed command’s substitution with the “'”, or the inline evaluation with the “)” - those two characters appear to be missing from the end of the line.

I’ve just logged on to my Mac and tried it and it seemed to give me what I think was the desired result based on your example PDF.

@sylumer

Sorry for the tardy reply ! Works absolutely perfect…

Appreciate the time and effort given… Excellent Group of skilled coders

Stay Safe