-1

I have a .csv file that has 2 columns: PRE and POST. The PRE column contains the old folder names and the POST column has the corresponding new folder names. I have about 1000 folders to rename and would like to automate it.

PRE,POST

123,123AB

003,234

456,789

Are you guys able to give me something to start with or point me to the right direction?

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
Poe
  • 13
  • 3
  • Is the value for each row a path to a directory or just the name? How are you planning to find out the location of the folder? or the folders exist in the same location of the csv file? – Luis Lavieri Jun 07 '19 at 17:23
  • @luis They're just the names. The folders exist in the same location of the csv file – Poe Jun 07 '19 at 17:54
  • 1
    Please post what have you tried. @idk already provided a naive solution. If you need something to start. Check [this](https://stackoverflow.com/questions/3507498/reading-csv-files-using-c-sharp) and [this](https://stackoverflow.com/questions/1941392/are-there-any-csv-readers-writer-libraries-in-c) posts if you need other ideas. – Luis Lavieri Jun 07 '19 at 18:23
  • FYI: [Are “Where to start?” / “Point me in the right direction” questions acceptable?](https://meta.stackexchange.com/questions/226103/are-point-me-in-the-right-direction-questions-acceptable) –  Jun 07 '19 at 18:36

1 Answers1

-1

Try this. It loops through all entries and renames the folders

using System.Linq;
using System.IO;

...

string csv = "csv path";
// Skip the one with the names
string[] items = File.ReadAllLines(csv).Skip(1);
foreach(var item in items)
{
    string oldname = item.Split(';')[0];
    string newname = item.Split(';')[1];
    if(Directory.Exists(oldname) && !Directory.Exists(newname))
        Directory.Move(oldname, newname);
}

untested but should work.

Owns_Rekt
  • 164
  • 11
  • Why split twice? What if the folder name has commas in it? What if the old folder does not exist? What if the new folder already exists? any logging to track the job? – Luis Lavieri Jun 07 '19 at 18:14
  • @luis You're right! It works until it runs into folders with the same name. Any suggestion? – Poe Jun 07 '19 at 19:04
  • I think your best option is to skip the ones that are duplicates. Assuming the process already renamed them to their new name. I am also assuming that all the new folders are unique in the file. – Luis Lavieri Jun 07 '19 at 19:28
  • Improved the answer. – Owns_Rekt Jun 07 '19 at 19:49
  • @idk - Thanks for that but the thing is, i dont want it to ignore the duplicates because eventually, the "same" folder will be renamed to something else. – Poe Jun 07 '19 at 21:07
  • If there's folder A, B and C and A gets renamed to B and later A gets renamed to C which folder should it be? B or C? – Owns_Rekt Jun 07 '19 at 21:09
  • Once A is renamed, it wont get renamed again. The concern is if A is renamed to B, it should allow it because B later on will get renamed to C. – Poe Jun 07 '19 at 21:25
  • When I run the code, A doesn't get renamed to B because "B" already exist. – Poe Jun 07 '19 at 21:38
  • Okay, I`ll fix that – Owns_Rekt Jun 08 '19 at 08:09
  • To understand: There's folder A B and C and A should be renamed to B and B to C? – Owns_Rekt Jun 08 '19 at 08:11