Search code examples
macosautomator

How do I Batch Rename Folders in OSX?


So I have been trying to rename about 5000 folders based on a CSV (Old name, Newname)

This is a one time operation, once hdkjsh2-2f8c-46b9-bbdb_doc is converted to 3 then it will not need to be touched again.

I have tried the solution here (Setting up an automator workflow with a command script) but found that it does not do a great deal when it comes to folder/directory names and all the guides/documentation is around file names and not folder. Any suggestions would be much appreciated

Example of CSV

Doc_Location,  New_ID
hdkjsh2-2f8c-46b9-bbdb_doc , 3

Solution

  • Please make a backup before trying the following.

    Save the following script in your HOME directory as renamer:

    #!/bin/bash
    
    cat "file.csv" | while IFS=' ,' read dir new ; do
       if [ -d "$dir" ]; then
          echo Rename $dir as $new
          #mv "$dir" "$new"
       else
          echo "ERROR: Directory $dir not found - ignored"
       fi
    done
    

    Then start Terminal and make the script executable by running:

    chmod +x $HOME/renamer
    

    Then change directory to where your directories are that need renaming:

    cd path/to/things/needing/renaming
    

    Make sure you have your CSV, called file.csv saved in that directory, then run with:

    $HOME/renamer
    

    It doesn't actually do anything, it just tells you what it would do. If it looks correct, edit $HOME/renamer and remove the single # on the line that says:

    #mv "$dir" "$new"
    

    so that is looks like:

    mv "$dir" "$new"
    

    Then be doubly sure you have made a backup and run the script again:

    $HOME/renamer