-1

Edit (to improve question):

Screenshot

In the image above you can see the naming of a series of files.

Each file has a filename of: 01-NameOfProject_Data Set X-01.png where X is sequential from 1 to 24 (please note! NOT 01, 02, ..., 24)

I have created two .csv files (to explore which works better upon your answer) as shown in the picture.

First step: I need to add the country-code prefix to each corresponding file. E.g. EN_01-NameOfProject_Data Set 1-01.png

Second step: I need to remove the part _Data Set X-01 The result would be EN_01-NameOfProject.png

I am kindly asking for help in the above two steps so I can better understand the procedure. I need to do this via macOS Terminal. I have install rename via Homebrew with brew install rename

Original question: I have a list of files that look like this:

01-NameOfProject_Data Set 1-01.png
01-NameOfProject_Data Set 2-01.png
01-NameOfProject_Data Set 3-01.png
...
01-NameOfProject_Data Set 24-01.png

I also have an Excel file (which I can save as .csv). It has a single column and exactly as many rows as the amount of the aforementioned files. Each cell has a language shortcode. It looks like this:

EN
GR
IT
...
BU

The Excel file in the correct (corresponding) order as the files meaning that: 01-NameOfProject_Data Set 1-01.png is indeed in English (EN) and so on.

How would you batch rename the files so you'd end up with something like:

EN_01-NameOfProject_new-text.png
GR_01-NameOfProject_new-text.png
IT_01-NameOfProject_new-text.png
...
BU_01-NameOfProject_new-text.png
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Kostis K.
  • 3
  • 3

1 Answers1

0

Like this, using Perl's rename:

brew install rename

Remove -n switch, aka dry-run when your attempts are satisfactory to rename for real.

#!/usr/bin/env bash

c=0 arr=( * )
while IFS=, read -r _ country; do
    rename -n "s/^(\d+).*/${country}_${1}NameOfProject_new-text.png/" "${arr[c++]}"
done < /tmp/file.csv

Output

rename(01-NameOfProject_Data Set 1-01.png, EN_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 2-01.png, GR_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 24-01.png, IT_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 3-01.png, BU_NameOfProject_new-text.png)

If order is not natural, then use:

#!/usr/bin/env bash

readarray -td '' files < <(
    ls --zero -v -- *.png
)
c=0
while IFS=, read -r _ country; do
    rename -0 -n "s/^(\d+).*/${country}_${1}NameOfProject_new-text.png/" "${files[c++]}"
done < /tmp/file.csv

Output

rename(01-NameOfProject_Data Set 1-01.png, EN_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 2-01.png, GR_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 3-01.png, IT_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 24-01.png, BU_NameOfProject_new-text.png)
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • Many thanks for your answer, I'm kindly asking for some clarifications as I am an absolute beginner, I am very sorry. Does this work in Terminal? I have moved to the correct dir but it doesn't seem to work. Does the "country" variable need to be set somehow in the csv file? Would the file.csv be in the same directory as the files to be renamed? – Kostis K. Mar 06 '23 at 20:03
  • Sure it works in terminal. This is the purpose of the script. Adapt `/tmp/file.csv` to the path of your `CSV` file. You can place it in the same dir, then instead of `/tmp/file.csv` use `file.csv`. What is the output of `rename --version` ? – Gilles Quénot Mar 06 '23 at 20:05
  • Sorry for the late reply. As unfortunately I can't make it work, I have updated the question to better clarify the occasion and added a screenshot too. I very much appreciate the help. – Kostis K. Mar 08 '23 at 12:52
  • Post edited accordingly. Next time(s), please provide the max informations from early beginning, and [please do not upload images of input/code/data/errors.](//meta.stackoverflow.com/q/285551) – Gilles Quénot Mar 08 '23 at 16:54
  • And for the **last time**, added useful link, you need **Perl version** of `rename`, thanks to read what I wrote... – Gilles Quénot Mar 08 '23 at 16:59