Bulk files renaming using csv tables

Hey guys

I have an issue with bulk renaming files

By using csv tables

A for old file name and b for the new file replacement


How can i patch renaming files ie images or psd or whatever format is

I need a sloution plz

Thanks



MacBook Air (2018 or later)

Posted on Jan 3, 2019 2:26 AM

Reply
11 replies

Jan 5, 2019 4:11 AM in response to VikingOSX

I went over to Windows 10 and created a CSV that was saved with Code page 1252 (Windows Western European) encoding. This had trailing CRLF line endings and when run through the above Run Shell Script, it produced the trailing ? on the renamed JPG images.


I have made a slight modification to the script to remove the carriage return part of each CSV line ending, and this will eliminate the trailing ? on the renamed images. Ideally you would save the CSV as UTF-8 text, but the fixes I have made should deal with Windows CRLF line endings.


Replace the contents of the Run Shell Script with the following updated script:


#!/bin/bash

cd "$1"

# simply exit if CSV file is absent or empty
[[ -s data.csv ]] || exit 1

# Strip quoted CSV fields (if present) and replace CRLF (if present) with LF
# 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 | tr -d '\r')
/usr/bin/osascript -e 'display dialog "Processing complete."' &> /dev/null
exit 0

Jan 3, 2019 4:14 PM in response to alsanidi01

I would recommend that your CSVFile be renamed to data.csv. The following Automator solution makes these assumptions:

  1. The data.csv file will be present among the images to be renamed.
  2. Header rows, and images in the OldImageName column that do not exist in the folder will be ignored without interruption in processing.
  3. If CSV data is double-quoted, these quotation marks will be removed before the row data is processed.
  4. This script is capable of dynamically adapting to different separator characters in the same CSV document instead of a single separator character. Just in case.
  5. The script makes no effort to deal with a Byte-order-mark (BOM) that MS Excel may place at the beginning of the CSV.


The only user interaction is to pick the folder whose contents are to be processed by the script, and to ensure that the correct CSV data is in that folder. This was tested on macOS High Sierra 10.13.6.


In the Run Shell Script action, replace the boiler plate text with the following Bash syntax:


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


The Automator application workflow will look like the following:

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 files renaming using csv tables

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