discodowney
discodowney

Reputation: 1507

Renaming a directory in C++

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?

Upvotes: 1

Views: 7283

Answers (3)

BillJam
BillJam

Reputation: 221

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

Upvotes: 0

betabandido
betabandido

Reputation: 19694

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

Upvotes: 1

Component 10
Component 10

Reputation: 10497

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.

Upvotes: 2

Related Questions