Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator - File rename script

Hello,


I'm trying to create a script that will rename files using a .csv file.



Here are the steps in Automator:



Here's the Shell Script (I got it from Tony T1)


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

if [ -e "$OldImageName" ] ; then

mv "$OldImageName" "$NewImageName"

else

echo "$OldImageName" >> ~/Desktop/Errors.txt

fi

done <"$2"


It runs well in Automator but for some reason the file names don't change.


Any help would be greatly appreciated :)

Posted on Jul 31, 2019 8:52 AM

Reply
Question marked as Best reply

Posted on Aug 1, 2019 9:41 AM

Ok. This script replacement will actually remove carriage returns (\015) characters (if they exist) but not newlines in each row. So it universally handles CSV from Windows or Mac as it was supposed to do before. Again, tested with your data and no "?" in renamed filenames this time. Also, ensure that every row in the CSV has a line ending, which would entail a blank last line, or the last entry in the CSV will not get renamed.


#!/bin/bash

cd "$1"
while read line       
do       
     OldImageName="${line%,*}"
     OldExt="${OldImageName##*.}"
     NewImageName="${line#*,}.$OldExt"
     # printf '%s\t%s\n' "$OldImageName" "$NewImageName"

     if [ -e "$OldImageName" ] ; then
          mv "$OldImageName" "$NewImageName"
     else
          echo "$OldImageName" >> ~/Desktop/Errors.txt
     fi
     # Remove CR but not NL
done < <(perl -lne '$_ =~ s/\015?//g && print;' "$2")
exit 0


Similar questions

32 replies

Automator - File rename script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.