1

Im trying to rename a directory to the same name as an existing directory in a test in C++. I have tried SHFileOperation. This actually works mostly. The problem is that we have an auotmated test and for some reason during the automated test the test fails here. Works fine on a manual run but the automated not so much. We think it might be that some dialog box is popping up even though i have the flags set to not allow any. i also tried rename the C++ function. rename doesnt seem to work when trying to rename something to an existing name.

So is there any other method i could use?

discodowney
  • 1,475
  • 6
  • 28
  • 58
  • 1
    Do you use absolute directory paths or relative paths? If the paths are relative, it may be that the automated test is launched from a different place, and the directory being renamed doesnt exist there. – Brady May 23 '12 at 12:40
  • 1
    I can't see how you could rename something and have the new name being the name of an existing object. That will always fail. – David Heffernan May 23 '12 at 12:41
  • http://stackoverflow.com/questions/3764822/how-to-durably-rename-a-file-in-posix – sehe May 23 '12 at 12:42
  • @DavidHeffernan not UNIX with mv(1), but I second the motion. He probably wants the 'overwrite' to be atomic as well – sehe May 23 '12 at 12:42
  • 1
    If you rename a directory to an existing directory name, basically the directories are merged – discodowney May 23 '12 at 12:48
  • OK, merging, didn't know that. The first thing to do is to check what error code `SHFileOperation` returns. – David Heffernan May 23 '12 at 13:07
  • Its not returning anything. It just hangs. Thats why we think it might be a dialog box popping up. We cant see if there is or not cos its in a different user session. I want to use a different method to see if there is some problem in the File System Driver i am testing with merging the folders when they are named the same – discodowney May 23 '12 at 13:40

3 Answers3

2

You could try boost::filesystem which has the advantage of portability if that's important to you.

namespace bfs=boost::filesystem;

std::string name("old_dir", "new_dir");

system::error_code ec = bfs::rename(name, new_name);

The ec is and error code object that can be referenced here so you could check for error conditions.

Component 10
  • 10,247
  • 7
  • 47
  • 64
1

I would definitely go for boost, but if you cannot use it, you must know that C++ does not have built-in support for such file operations.

The best you can do is use C's rename():

http://en.cppreference.com/w/c/io/rename

betabandido
  • 18,946
  • 11
  • 62
  • 76
0

You could try MoveFile which also work for directories. Though I doubt if any API is able to rename something to a name that already exists. https://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx

BillJam
  • 221
  • 3
  • 13