bulk file rename using csv

Hey guys,


I've been trying to use the automator workflow VikingOSX provided but with no luck. I get a process completed message with no errors, but the file name does not change. I'm using this workflow below, my csv is data.cxv and is UTF-8. not sure what im doing wrong here, any help is appreciated.


cd "$1"


# simply exit if CSV file is absent or empty

[[ -s data.csv ]] || exit 1


# Strip quoted CSV fields (if present) before reading the data

# Dynamically adapt IFS to different CSV delimiters

while IFS=$'\t,;:!' read oldfile newfile

do

# Skip entry if old filename in CSV doesn't exist in current folder

[[ -f "./$oldfile" ]] || continue

# printf '%s\t%s\n' "$oldfile" "$newfile"

# rename

mv $PWD/{"$oldfile","$newfile"}

done < <(sed -e 's/"//g;' < ./data.csv)

/usr/bin/osascript -e 'display dialog "Processing complete."' &> /dev/null

exit 0



MacBook Pro with Touch Bar

Posted on Sep 2, 2020 7:28 PM

Reply
Question marked as Top-ranking reply

Posted on Sep 4, 2020 8:03 PM

My last post had a syntax error in it which I didn't catch until later this evening. The following script only processes CSV with comma separated values, and as before, fixes trailing commas, or replaces the forward slash in names with an underscore. Again, tested with your old, and new filenames as data:


DIR="$1"
shift
CSV="$1"

# unnecessary, but if the CSV file doesn't exist, exit
# [[ -s "$CSV" ]] || exit 1

cd "${DIR}"

# works with comma delimited CSV
while IFS=',' read oldfile newfile
do
	# do not process headings
	[[ ! "${oldfile}" =~ \.(jpg|jpeg|png)$ ]] && continue

    # Skip entry if old filename in CSV doesn't exist in current folder
    [[ -e "${oldfile}" ]] || continue

	# strip any trailing empty comma delimiters from end of newfile
	newfilex="${newfile//,/}"
	# replace any detected illegal forward slash with an underscore
	newfilex="${newfilex//\//_}"

    # rename files
    /bin/mv "${oldfile}" "${newfilex}"
# remove any double-quoted items and if from Windows, trim the CR from the line
done < <(sed -e 's/"//g;' "${CSV}" | tr -d '\r')
# done < "${CSV}"

sleep 1
/usr/bin/osascript -e 'display dialog "Processing complete."' &> /dev/null
exit 0


I am not for hire, as I participate here voluntarily for no compensation. You never want to post personal contact information in the Apple Support Communities, because if the Apple hosts do not remove it soon enough, it will be forever available in the Google archives for trolling spammers.


There are others that I am helping, and my time is limited to the code that I have already provided.

23 replies

Sep 3, 2020 10:09 AM in response to mnagourney

The following works on Mojave 10.14.6 (18G6020) as an Automator application. It prompts twice: 1) image folder, and 2) CSV file. I have enhanced it to automatically deal with Windows line endings in the CSV (if encountered), ignore any CSV header row, provided it finds oldfile in the header, and as previous, trims any double-quotes from the column items.



Here is the code for the Run Shell Script:


DIR="$1"
shift
CSV="$1"

# unnecessary, but if the CSV file doesn't exist, exit
[[ -s "$CSV" ]] || exit 1

cd "${DIR}"

# Dynamically adapt IFS to different CSV delimiters

# while IFS=$'\t,;:!' read oldfile newfile
while IFS=, read oldfile newfile
do
	# do not process headings
	[[ oldfile = "oldfile" ]] || continue

    # Skip entry if old filename in CSV doesn't exist in current folder
    [[ -e $oldfile ]] || continue
  
    #printf '%s\t%s\n' "$oldfile" "$newfile" > ~/Desktop/mycsv.txt

    # rename.
    mv "${oldfile}" "${newfile}"
# remove any double-quoted items and if from Windows, trim the CR from the line
done < <(sed -e 's/"//g;' "${CSV}" | tr -d '\r')

sleep 1
/usr/bin/osascript -e 'display dialog "Processing complete."' &> /dev/null
exit 0


Sep 3, 2020 6:43 PM in response to mnagourney

Replace the Do Shell Script code with the following content. It is tested using your updated CSV data items, and works fine here.


DIR="$1"
shift
CSV="$1"

# unnecessary, but if the CSV file doesn't exist, exit
# [[ -s "$CSV" ]] || exit 1

cd "${DIR}"

# Dynamically adapt IFS to different CSV delimiters
while IFS=$'\t,;:!' read oldfile newfile
do
	# do not process headings
	[[ ! "${oldfile}" =~ \.(jpg|jpeg|png)$ ]] && continue

    # Skip entry if old filename in CSV doesn't exist in current folder
    [[ -e "${oldfile}" ]] || continue
  
    # printf '%s\t%s\n' "$oldfile" "$newfile" > ~/Desktop/mycsv.txt

    # rename.
    /bin/mv "${oldfile}" "${newfile}"
# remove any double-quoted items and if from Windows, trim the CR from the line
done < <(sed -e 's/"//g;' "${CSV}" | tr -d '\r')
# done < "${CSV}"

sleep 1
/usr/bin/osascript -e 'display dialog "Processing complete."' &> /dev/null
exit 0


Sep 4, 2020 10:47 AM in response to mnagourney

A proper CSV does not have multiple, trailing empty fields in it, which your five commas represent. I have altered the script to automatically trim those commas from the newfile variable if present.


In the third category, the forward slash in the output filename, whether unescaped, or escaped, is invalid, and I have added code to replace that forward slash with an underscore, which does allow the script to process the third category.


DIR="$1"
shift
CSV="$1"

# unnecessary, but if the CSV file doesn't exist, exit
# [[ -s "$CSV" ]] || exit 1

cd "${DIR}"

# Dynamically adapt IFS to different CSV delimiters
while IFS=$'\t,;:!' read oldfile newfile
do
	# do not process headings
	[[ ! "${oldfile}" =~ \.(jpg|jpeg|png)$ ]] && continue

    # Skip entry if old filename in CSV doesn't exist in current folder
    [[ -e "${oldfile}" ]] || continue

	# strip any trailing empty column delimiters from end of newfile
	newfilex="${newfile//[,;:!\t]/}"
	# replace any detected illegal forward slash with an underscore
	newfilex="${newfilex//\//_}"

    # rename files
    /bin/mv "${oldfile}" "${newfilex}"
# remove any double-quoted items and if from Windows, trim the CR from the line
done < <(sed -e 's/"//g;' "${CSV}" | tr -d '\r')
# done < "${CSV}"

sleep 1
/usr/bin/osascript -e 'display dialog "Processing complete."' &> /dev/null
exit 0


Sep 5, 2020 5:59 PM in response to mnagourney

The last code that I posted only accepts a true CSV with commas for delimiters. I have tested with your old and new filename examples, including those with five trailing commas indicating five empty columns. The code works here and renames correctly.


Don't be appending different CSV content with different delimiters to the same file, that will surely gum things up. I have code in place to remove double-quoted strings. The script also handles Windows and Mac row endings.


Post a few lines of text here representing the oldname,newname of the CSV content that does nothing. Let me test it on my end to see if there is something causing the issue.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

bulk file rename using csv

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