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

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 Best answer

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.

Posted on Sep 4, 2020 8:03 PM

23 replies
Question marked as Best answer

Sep 4, 2020 8:03 PM in response to mnagourney

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.

Sep 5, 2020 4:52 PM in response to VikingOSX

No worries at all.


Still having the same result though, no change at all in file names.


Seems like it's something other than the code perhaps? When I was playing around with other scripts before starting this thread, I had two occasions where the script successfully changed the file names and then I couldn't repeat the action again. Same as what's going on here. Ever since I ran the script that only converted 2/3 of the files and added the commas, nothing I've run has made any changes. Any idea what might be causing that?



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.

Sep 5, 2020 6:34 PM in response to mnagourney

It checks if the name of the old file has a .png, .jpg, or .jpeg extension, and headings would not have that, so it skips the heading row. If you are trying to use different file extensions, then it would skip all of those rows. It should not be skipping the first data row, unless there is another case of an invalid (to the shell) character in a filename, or the first row does not use a comma delimiter between old and new filenames.

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 ID.