CT_Mike

Q: Batch file rename

I am setting up a new NAS as a media server. When I copied over my music files the process hung up for some reason and didn't finish, other than creating the folders. When I started over, it named all of the new folders "artist 2" as overwriting the existing empty folders. Is there a way to batch rename all of these folders? I tried using Automator but it doesn't accept wildcards apparently.

 

I tried to Find: * 2 and Replace with *.

 

No joy.

 

Thanks.

iMac, Mac OS X (10.7.2)

Posted on Jan 22, 2016 11:54 AM

Close

Q: Batch file rename

  • All replies
  • Helpful answers

  • by rkaufmann87,

    rkaufmann87 rkaufmann87 Jan 22, 2016 12:25 PM in response to CT_Mike
    Level 9 (58,073 points)
    Photos for Mac
    Jan 22, 2016 12:25 PM in response to CT_Mike

    Automator can do that. Google for an Automator script to learn how to program it. If you were using an up-to-date version of OS X for example Yosemite or El Capitan it can also be done there by right clicking the files.

  • by CT_Mike,

    CT_Mike CT_Mike Jan 22, 2016 12:54 PM in response to rkaufmann87
    Level 1 (10 points)
    Jan 22, 2016 12:54 PM in response to rkaufmann87

    I am using Yosemite. I used Automator to build a script as follows:

     

    - Get Selected Finder Items

    - Rename Finder Items: Replace Text

       In the Find field I put "* 2" (no quotes)

       In the Replace field I put "*" (again no quotes)

     

    The workflow ran but did not process any files. I did try the script with Find "Aerosmith 2" and replace with "Aerosmith" and it worked fine. It just appears that automator doesn't like wildcards.

  • by rkaufmann87,

    rkaufmann87 rkaufmann87 Jan 22, 2016 1:10 PM in response to CT_Mike
    Level 9 (58,073 points)
    Photos for Mac
    Jan 22, 2016 1:10 PM in response to CT_Mike

    I believe I have done it, check the workflow in http://www.cnet.com/how-to/how-to-batch-rename-files-using-automator-in-os-x/ to see if that works. If it does not reach out to the author, Topher and he may reply. You can also reach him on his own site at http://www.macissues.com/

  • by Tony T1,

    Tony T1 Tony T1 Jan 24, 2016 6:22 AM in response to CT_Mike
    Level 6 (9,225 points)
    Mac OS X
    Jan 24, 2016 6:22 AM in response to CT_Mike

    This bash script in an Automator Workflow should work.

    (Backup and try on test files first)

     

    This is written to strip a space and a single digit at the end of a folder name

     

    Screen Shot 2016-01-24 at 9.16.26 AM.png

     

    The Run Shell Script Action is:

    (Note: select: Pass input: [as arguments] in the Action)

     

    for f in "$@"

    do

      newFolderName=$(echo "$f" | sed -E 's/ [0-9]$//')

      if [[ "$f" != "$newFolderName" ]]; then

           if [[ ! -d "$newFolderName" ]]; then

                mv "$f" "$newFolderName"

           fi

      fi

    done

  • by Tony T1,

    Tony T1 Tony T1 Jan 24, 2016 7:52 AM in response to Tony T1
    Level 6 (9,225 points)
    Mac OS X
    Jan 24, 2016 7:52 AM in response to Tony T1

    If you only need to strip " 2" from the end of a Folder name (and not space and any digit)

    change:

         newFolderName=$(echo "$f" | sed -E 's/ [0-9]$//')

    to:

         newFolderName=$(echo "$f" | sed 's/ 2$//')

  • by CT_Mike,

    CT_Mike CT_Mike Jan 24, 2016 2:48 PM in response to CT_Mike
    Level 1 (10 points)
    Jan 24, 2016 2:48 PM in response to CT_Mike

    Tony T1,

     

    Thanks man, your script worked awesome, with a little modification.

     

    I appreciate the help.

  • by Tony T1,

    Tony T1 Tony T1 Jan 24, 2016 3:48 PM in response to CT_Mike
    Level 6 (9,225 points)
    Mac OS X
    Jan 24, 2016 3:48 PM in response to CT_Mike

    Glad it worked,  What did you modify?

  • by CT_Mike,

    CT_Mike CT_Mike Jan 24, 2016 4:46 PM in response to Tony T1
    Level 1 (10 points)
    Jan 24, 2016 4:46 PM in response to Tony T1

    I got rid of the "Get Folder Contents" and "Filter Finder Items" because those two steps caused it for hang up for some reason. Then I just had to manually select the folders, but that was much better than manually having to edit hundreds of folder names individually.

     

    Thanks for the help.

  • by Tony T1,Apple recommended

    Tony T1 Tony T1 Jan 25, 2016 7:03 AM in response to CT_Mike
    Level 6 (9,225 points)
    Mac OS X
    Jan 25, 2016 7:03 AM in response to CT_Mike

    Not sure why the Automator Action to get the folders failed, however, there are other ways to walk a directory tree.

    I realize that you don't need this now, but here's another method:

     

    Screen Shot 2016-01-25 at 9.57.53 AM.png

     

    The Run Shell Script Action is:

     

    function walk_tree {

      local directory="$1"

      local i

      for i in "$directory"/*;

      do

           if [ "$i" = . -o "$i" = .. ]; then

                continue

           elif [ -d "$i" ]; then

                newFolderName=$(echo "$i" | sed 's/ 2$//')

                if [[ "$i" != "$newFolderName" ]]; then

                     if [[ ! -d "$newFolderName" ]]; then

                          mv "$i" "$newFolderName"

                     fi

                fi

                walk_tree "$i"

           else

                continue     # replace continue with command to process individual file "$i" (i.e. echo "$i")

           fi

      done

    }

     

    walk_tree "$1"