-3

i need a script which will rename large amount of files. I got a folder with a lot of files. Every file is named by ID. Then i have a CSV file like this:

oldID;newID

oldID;newID

etc...

Every old and new id is specific and original. I'd like to ask what should be the best way to do it or little help in bash/batch.

Community
  • 1
  • 1
Cart
  • 23
  • 3
  • Does your comma separated values (CSV) file separate values by semicolon? – e0k Jan 23 '16 at 22:04
  • Is it bash or batch? Bash is used by Linux and Batch is used by Windows. – SomethingDark Jan 23 '16 at 22:04
  • it is exactly as i wrote. Every line == two numbers separated by semicolon. For this project, I prefer batch. Thanks! – Cart Jan 23 '16 at 22:31
  • 1
    I just posted an answer for **bash** then now read your desire for a **batch** version. Please be specific in your questions about what you want and use the appropriate tags. (If you use the [tag:bash] tag, you will get people who want to answer bash questions.) – e0k Jan 23 '16 at 22:36
  • 2
    You prefer batch but you accepted the bash answer? Make up your mind! – SomethingDark Jan 23 '16 at 23:29

3 Answers3

4

The solution for batch is very similar to e0k's solution for bash; you read the file in one line at a time, split the line on semicolons, and rename the file accordingly.

for /f "tokens=1,2 delims=;" %%A in (ids.csv) do ren "%%A" "%%B"

This assumes that your IDs are in a file called ids.csv

SomethingDark
  • 10,902
  • 5
  • 44
  • 47
0

If you are using bash (the shell used in world of Linux, UNIX, etc.), you can use the following short script based on this internal field separator answer. This assumes that you are using a semicolon (;) as the delimiter of your "CSV" file and that there is only one such delimiter.

#!/bin/bash

while IFS=';' read -ra names; do
    mv "${names[0]}" "${names[1]}";
done < translation.csv

where translation.csv is your file containing the name translations with an oldname;newname format.

If you are instead asking for a batch file (i.e. for Windows, DOS, etc.) then that is a different animal in a different world.

Community
  • 1
  • 1
e0k
  • 6,246
  • 2
  • 18
  • 28
0

Given that your OS is some unix (like linux), and given that the use of csv files has been your own choice, there might be an easier way to go: mmv can rename many files in one go, using patterns to match original files, and allowing to use the matched strings in the target file names. See http://ss64.com/bash/mmv.html.

Dirk Herrmann
  • 4,571
  • 1
  • 14
  • 39