Rename batch of file

Hello all, I am new to the forum and more importantly new to Automator.
I hope someone familiar with automator can help or at least point me in the right direction.

I have several hundred file in a single folder that I need to rename.

an example of the current file name is:
_GAR0538_641f9febe94f462884ff70a4d969c47f.NEF

I need to rename to the original file name, in this case:
_GAR0538.NEF

any help would be appreciated, I don’t look forward to changing these name manually.

thanks
Geoff

If this is a repeating issue, I would set up Hazel to do it. If it’s a single use I would check to see if you can do it in Finders batch rename feature (depends on your requirements), if it doesn’t work then I would use BBEdit on a listing, do a search replace and create a simple shell script that have a lot of mv oldfilename newfilename and run the resulting shell script, or if you know about shell scripts write a small script that does the same thing.

There are commercial software products that do this kind of thing. I happen to use Name Mangler and like it. They offer a free trial to get familiar with their software. If this is a one-time situation for you, you could try the software and solve your problem. If you anticipate running into similar situations in the future, then I would recommend buying a utility, such as Name Mangler, designed for the purpose.

There are lots of ways to manipulate file names on the command line. That is very much what computers and command lines were designed to do from very early on.

Because you are doing a pattern-based substitution, the process certainly has some complexity to it for a beginner, so here is a command that if you enter it in the terminal while you are in the folder containing your files to be renamed, should rename those files as per your request.

As always, take a backup before you start playing with this stuff, just in case.

ls _*_*.NEF | sed -n 's/\(.*\)\(_\)\(.*\)\(\.NEF\)/mv "\1\2\3\4" "\1\4"/p' | sh

The premise is it builds a set of rename instructions fro each file match and rename, and then executes them.

In Automator, create an application, add a shell script step and set the action to be arguments. Add in a line cd $@ to change the directory and the line above to do the processing.

Save the Automator application. Now when you drop the folder onto the application in Finder, it should do all of the renaming.

You could modify this to a quick action, change it to work on an individual file rather than folder, set it as a folder action that will run automatically (like Hazel, but not as user friendly or maintainable - I use Hazel for this reason), or even migrate it to another automation.

That should be enough to get you going.

I guess I should also point out that Apple announced this week that Shortcuts will be the successor to Automator in the next OS release for Mac. It will stick around for a while to come, as people migrate to Shortcuts, but its use will be deprecated. While it will likely scratch an itch today, that itch scratch will likely fall to Shortcuts tomorrow.

Hope that helps.

With all due respect to @sylumer, I’m going to suggest a shorter shell script that takes advantage of a Z Shell feature. It uses the zmv command, which combines all three parts of his pipeline. The command itself is

zmv '_(*)_*.NEF' '_$1.NEF'

This gets all the files that fit the input pattern (underscore, something, underscore, something else, NEF extension) and renames them to the output pattern (underscore, something, NEF extension). The parentheses in the input pattern saves the part between the underscores to use where $1 appears in the output pattern.

In an Automator Quick Action it will look like this:

The only tricky thing is that you have to include autoload zmv before calling zmv.

As with @sylumer’s Quick Action, you run it on a folder of files you want to rename.

1 Like

I’m all for simplification :nerd_face:

I based my answer on something I’d used as a principle in bash.