1

is there any way to rename a file automatically by Automator, removing first 6 characters and moving them to the back of the file name (and adding a space before that)?

Example 20-10-03 Testing.zip

Rename to: Testing 20-10-03.zip

Thanks!

1
  • 1
    When comparing the example filename to the title and body of your question, it is not consistent. While you are saying to remove the first 6 characters from the front of the filename, the example clearly shows you removing the first 9 characters and my answer conforms to the example filename. May 19, 2020 at 5:49

2 Answers 2

2

Yes, one of the ways it can be done in Automator is by using a Run Shell Script action with the example bash script code shown further below.

You can create an Automator Workflow, or Service/Quick Action, workflow.

If you do just a Workflow, then you will need to add a Files & Folders action, e.g. Get Selected Finder Items or Get Specified Finder Items, and then add a Run Shell Script action.

If you do a Service/Quick Action, then you only need a Run Shell Script action.

If you do choose a Service/Quick Action then set it to use files and folders in Finder.

For the Run Shell Script action, have its settings configured as:

  • Shell: /bin/bash
  • Pass input: as arguments
  • Replace the default code of the Run Shell Script action with the example bash script code.

The example bash script code uses shell parameter expansion to slice and dice the fully qualified pathname of the file(s) passed to it into the necessary pieces and then renames the file(s) using the mv command with the -n option, which will not overwrite an existing file, based on the values of the sliced and diced pieces of the fully qualified pathname.

As coded, it uses a regex to only act on files that starts with two digits, followed by a dash, followed by two digits, followed by a dash, followed by two digits, followed by a space, followed by additional characters and a file extension, e.g.: 20-10-03 Testing.zip

Example bash script code:

    # f = fully qualified pathname
    # d = directory pathname
    # fn = filename with extension
    # n = filename without extension
    # e = filename extension
    # p = filename prefix e.g. '20-10-03'
    # s = filename suffix (filename without prefix, space, and extension)


for f in "$@"
do
    [ -f "${f}" ] || continue
    d="${f%/*}"
    fn="${f##*/}"
    [[ ${fn} =~ ^[0-9]{2}-[0-9]{2}-[0-9]{2}[[:space:]].*\..* ]] || continue
    n="${fn%.*}"
    e="${fn##*.}"
    p="${n%${n#????????}}"
    s="${n:9}"
    [ -n "${s}" ] || continue
    mv -n "${f}" "${d}/${s} ${p}.${e}"
done


The output of which would be, e.g.: Testing 20-10-03.zip


Note: The example bash script code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.

2
-1

This answer might be what you're looking for. You need regex, or a shell script using regex working with Automator.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .