I have a code which should copy a file but it doesn't work

I have a code which should copy d://folder//(unknownDir)//black.txt to d://folder//red//white.txt but it doesn't work and I don't understand why. I'm trying a code with copy function of windows.h (path variable doesn't work). Do you have any clue what's wrong here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "pch.h"
#include <Windows.h>
#include <memory>
#include <filesystem>
#include <algorithm>
#include <iostream>
#include <tchar.h>

namespace fs = std::filesystem;

int main()
{
    try {
        std::for_each(fs::recursive_directory_iterator("d:/folder/"), {},
            [](fs::directory_entry const& dirent)
        {
            if (fs::is_regular_file(dirent) &&
                dirent.path().filename() == "black.txt") 
                fs::copy(dirent, "d:/folder/");
                TCHAR path[_MAX_PATH];
                _tcscpy(path, dirent.path().filename().string().c_str());
                CopyFile(
                    path,
                    "d://folder//red//white.txt", // Hardwire the path\filename
                    TRUE // Do not overwrite it if it already exists.
                );
            }

        );
    }

    catch (fs::filesystem_error const& e)
    {
        std::cerr << "error: " << e.what() << '\n';
    }
}
This is some kind of confused there.

It is simpler to recurse through the directories with something like:

1
2
3
4
   for( auto & dirent : fs::recursive_directory_iterator("d:/folder/") )
        {
          ...
        }


Next, avoid the Windows platform, since you're already using fs, use fs::copy (you have both active here)

1
2
3
4
5
   for( auto & dirent : fs::recursive_directory_iterator("d:/folder/") )
        {
            if (fs::is_regular_file(dirent) && dirent.path().filename() == "black.txt") 
                fs::copy(dirent, "d:/folder/red/white.txt");
        }
But I want to use it somewhere else, not only in coopy file function
Why would this form bind you to only the copy function?

It doesn't.

Can you be more specific about what you mean?
I just debugged and it says that imgur.com/a/Ul4f4ua and I can't understand why
a.txt was in my "folder". Now I deleted it and now it shows that "path" is equal to some weird symbols
What code are you running?

In the code I posted there are no problems, but care must be taken with the configuration of the "character set" in a Windows build.

"Some weird symbols" isn't much information to go on, but I do recognize the results are "easier" to deal with in a Unicode build as opposed no MCBS or "not set" modes (ASCII).

In order to help you we must see the exact code you're commenting about, and that can't be the original code you posted, it is performing multiple copies with confusion as to what character set you're configured for.

Windows, by default, uses Unicode (which wasn't a good choice), so the string literals shown in your code (and the example I posted) are not "tagged" to mutate properly.



Topic archived. No new replies allowed.