-2

This part of my programm tries to rename all folders and subfolders. All other functionality is in another code, here I'm just renaming a single folder by providing a path.

Since rename doesnt seem to work for me I tried using MoveFile.

I understand that it requires an LPCTSTR value.. but the paths I am currently providing (casted from std::filesystem::directory_entry -> std::filesystem::path) -> LPCTSTR) aren't accepted.

I'm getting that I'm not casting it the right way and I probably have to provide it with an "L" in front of the variable, but I can't find nor figure out the syntax.

enter image description here

bool renameFolder(std::string confirmStr3, auto dirEntry, std::string& replacedStr, std::string& insertStr, int& foldername_replacements)
{
    std::string path_string = dirEntry.path().string();

    path_string = std::filesystem::path(path_string).filename().string();

    replace_all(path_string, replacedStr, insertStr);

    path_string = getFolderPath(std::filesystem::path(dirEntry).string()) + "\\" + path_string;
 

    if (std::filesystem::path(dirEntry) != std::filesystem::path(path_string))
        foldername_replacements++;


    //std::filesystem::rename(std::filesystem::path(dirEntry), std::filesystem::path(path_string));
    MoveFile(LPCTSTR(std::filesystem::path(dirEntry)), LPCTSTR(std::filesystem::path(path_string)));

}
E_net4
  • 27,810
  • 13
  • 101
  • 139
Gisbert12843
  • 76
  • 1
  • 10
  • 1
    [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) – Remy Lebeau Aug 02 '22 at 07:20
  • I didn't downvote you. – Remy Lebeau Aug 02 '22 at 07:26
  • 1
    Have you read the [help] yet? Particularly [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Remy Lebeau Aug 02 '22 at 18:13
  • Ok I realized it was you again :D Yes, I did read it. I mark everything I feel.. I only forgot to include the code the right way.. Oh and yeah Ill ignore to not post the error messages.. thats just plain dumb imo.. It's a short question and errormessages are very important for people that don't own a biological interpreter in their body. – Gisbert12843 Aug 03 '22 at 08:29

1 Answers1

2

You can't type cast a std::filesystem::path object directly to a character pointer. That is exactly what the error message is telling you. And you can't use the L prefix with variables, only with compile-time literals.

You need to call the path::c_str() method instead, eg:

MoveFileW(std::filesystem::path(dirEntry).c_str(), std::filesystem::path(path_string).c_str());

Or, call the path::(w)string() method, and then call c_str() on the returned std::(w)string object, eg:

MoveFileW(std::filesystem::path(dirEntry).wstring().c_str(), std::filesystem::path(path_string).wstring().c_str());

That being said, std::rename() is likely to be implemented on Windows using MoveFile/Ex() internally, so this is a possible XY Problem. std::rename() is the preferred solution, so you should focus your efforts on figuring out why it is not working for you.


UPDATE:

On a side note, the code you have shown makes repetitive use of temporary std::filesystem::path objects that are unnecessary. Try simplifying the code, like this:

bool renameFolder(std::string confirmStr3, auto dirEntry, std::string& replacedStr, std::string& insertStr, int& foldername_replacements)
{
    auto dirEntryPath = dirEntry.path();
    auto file_name = dirEntryPath.filename().string();

    replace_all(file_name, replacedStr, insertStr);

    auto newDirEntryPath = dirEntryPath / file_name;    
    if (dirEntryPath != newDirEntryPath)
    {
        ++foldername_replacements;

        //std::filesystem::rename(dirEntryPath, newDirEntryPath);
        MoveFileW(dirEntryPath.c_str(), newDirEntryPath.c_str());
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • yeah i think i get you! thank you very much! :) – Gisbert12843 Aug 02 '22 at 07:24
  • Hey, maybe you'll read this. Wanted to thank you again for making me aware of the possible XY Problem, better the overall concept of it! :D It fixed a design flaw and brought me further in my studies <3 @remy-lebeau – Gisbert12843 Aug 02 '22 at 10:32
  • I will look into that! Thanks for the insight! :) Got the project ready and done by now. It just renames all files, folders and content in less than a second, but Optimisation especially in C++ is always very important, so thanks for that! If someone wants to copy it (though its prob bad code :D), you can find it here https://github.com/Gisbert12843/FindAndReplace. – Gisbert12843 Aug 03 '22 at 08:36
  • Don't yet understand why, but that code as it is and even in versions I altered a bit just doesn't work.. Doesn't matter if MoveFile or MoveFileW is used too mhhh It just won't rename the folder, but says it does :D I'll update this if I figure it out. – Gisbert12843 Aug 03 '22 at 10:13
  • Define "doesn't work" exactly. You need to be more specific. Is `rename()` returning an error code, or throwing an exception? Is `MoveFileEx()` returning false, and if so then what does `GetLastError()` return? – Remy Lebeau Aug 03 '22 at 15:53
  • Didn't fully debug it tbh.. it's not returning any errors at compile or runtime.. it just won't rename the folder ^^ I would guess it has something to do with formatting the path.. we had like 36°C the last 3 days so I didn't have the nerves to work that much :S will do that later to learn from it though! – Gisbert12843 Aug 04 '22 at 10:40
  • @Gisbert12843 it can't fail to rename and not report an error. The only way I could think of for it to report success but still not rename is if the folder/file is in use and the OS is waiting for that to be released before then renaming it. Unless your debugging is faulty and you are simply diagnosing it wrong. – Remy Lebeau Aug 04 '22 at 18:11
  • What i meant is, that it doesn't throw an exception of any kind.. I didn't debug it fully yet ^^ It just doesn't rename the folders but just seems to skip it.. you don't have to do more on that :D you really helped me more than enough! thx a bunch!! – Gisbert12843 Aug 04 '22 at 20:19