0

So I am re writing a routine to rename stuff. My Folder tree structure looks similar to this:

Folder  ->  Folder(s)   -> Folder(s) + ImageFile(s)

Session ->  Location(s) -> Test(s)   + Images

All locations have the same Tests and renaming one must rename all. Also, Every Image has a related folder named the same as that image. So Images "one", "two", "three" will have folders "one", "two", "three" together in the same directory (location).

I am using this code to rename:

DirectoryInfo MainSessionFolder = new DirectoryInfo(FileStructure.CurrentSessionPath); // Main Session Folder
DirectoryInfo[] locations = MainSessionFolder.GetDirectories(); // Get all locations

foreach(var location in locations) // for every location
{
    DirectoryInfo[] TestFolders = location.GetDirectories(); // get test folders in that location
    foreach (var TestFolder in TestFolders) // for every test folder
    {
        if(SelectedTest.Name == TestFolder.Name) // check it it's what we want to rename
        Directory.Move(TestFolder.FullName, TestFolder.Name.Replace(SelectedTest.Name, NewName)); // rename it
    }

    FileInfo[] AllFiles = location.GetFiles(); // get all files in that location
    foreach (var ThisFile in AllFiles) // for every file in that location
    {
        if (SelectedTest.Name == ThisFile.Name) // check
            File.Move(SelectedTest.Name, NewName); // rename
    }
}

It pops an error on the DirectoryInfo.move (not FileInfo.move, weird because error says file) saying:

An exception of type 'System.IO.IOException' ....
.. Cannot create a file when that file already exists ..

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • What is the value of `text`? Have you run it in the debugger to make sure you're not trying to rename two different folders to the same name? – D Stanley Apr 04 '16 at 18:00
  • @Dstanley 'text' which is 'NewName' now in the post is the entered string from the user. Definitely can't happen because one folder at a time and it will happen only once – Khalil Khalaf Apr 04 '16 at 18:02

1 Answers1

1

It sounds like your move from and to are pointing to the same location.

Try temporarily breaking down your Directory.Move statement into smaller parts and step through to check the all values are what you expect.

string target = TestFolder.Name.Replace(SelectedTest.Name, NewName);

Then check the values of TestFolder.FullName, TestFolder.Name, SelectedTest.Name, NewName, target

Check they're what you expect, and also check in explorer that the target directories don't already exist.

Should hopefully give you an idea of what is going wrong.

Tone
  • 1,701
  • 1
  • 17
  • 18
  • Definitely helped. Dump that I missed that (without lunch yet maybe?). Turned out both should be FullName on Directory.move. Also, there is no File.FullName, so I had to use this instead of the very last line: File.Move(location.FullName + "\\" + SelectedTest.Name + ".png", location.FullName + "\\" + NewName + ".png"). And becuase of debuggin, a folder was created already in bin/debug and an error is being poped up. I wasn't working in my actual directory, instead I was working on bin/debug cz Name instead of FullName. Edit your answer so I mark it. And thanks :) – Khalil Khalaf Apr 04 '16 at 18:34
  • Ah good, glad it helped. I've updated the answer to reflect the question updates. I assume that was the only update you were looking for? If you wanted the solution including that would probably be better as an update on your question (I can add it to the answer if you'd prefer - just let me know the exact syntax you used). – Tone Apr 05 '16 at 01:30