1

Today I'm struggling on a problem that was already half answered on other posts.

Here is what I try to do in c++ using dos command through the system command:

command = "s: && cd " + all_paths[i] + " && rename \"" + linestring + "\" \"" + completeCorrectName+"\"";
//what it really contain is => "s: && cd S:\\Holliday\\Spain\\ && rename \"Spain - été 2018 !.mkv\" \"Spain - pool 2018.mkv\""
system(command.c_str());

This is a dos command that I launch through a c++ program so I can use a lot more native functions.

Everything is working fine ! Except that I cannot rename a file that is containing special characters. Because of that, I get an error: "Specified file unreachable". And that is because special characters are memorized in my string variable as this:

"maŒtre et ‚lŠve.mkv"

So I tried "wstring", I tried "#Define UNICODE" and also "#Define _UNICODE"... Nothing works.

EDIT: I used cmd/dos because of the dir method which is usefull. I save the dir method like this:

command = "s: && cd " + all_paths[i] + " && dir /a-d /o-d /b *";
            FILE* fpipe = _popen(command.c_str(),"r"); // run dir command and save it inside fpipe => memory file
            if (fpipe) // If we can read it successfully
            {
                char line[500];
                string linestring;
                while (fgets(line, 500, fpipe)) // looping on each line
                {
                    linestring.clear();
                    for (int j : line) //Convert buffer in string
                    {
                        linestring.push_back(j);
                    }
              }
Impact
  • 31
  • 6
  • 2
    Unless your computer is over 19 or so years old, you are not using DOS. The last version of Windows that included a version of DOS was Windows Me. – JaMiT Nov 01 '20 at 23:05
  • 1
    By default, command prompt (replacement of DOS) does not use the same charater encoding (code page) as Windows. If you are using a recent version of Windows, UTF-8 could works everywhere. You also need to be aware of the encoding of the C++ file (generally either ANSI or UTF-8). If your file is not in the same encoding as the target system, you need conversion. – Phil1970 Nov 01 '20 at 23:05
  • Unicode output in Windows is very complicated. Look at this: https://stackoverflow.com/a/9051543/2193968 And if that doesn't work have a look at this: https://www.codeproject.com/Articles/34068/Unicode-Output-to-the-Windows-Console – Jerry Jeremiah Nov 01 '20 at 23:06
  • Related: https://devblogs.microsoft.com/commandline/windows-command-line-unicode-and-utf-8-output-text-buffer/ – Jerry Jeremiah Nov 01 '20 at 23:08
  • 3
    If you're just trying to rename the file, you can use [`MoveFile`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefile). – 1201ProgramAlarm Nov 01 '20 at 23:11
  • @JaMiT even if I try `if (rename(command.c_str(), completeCorrectName.c_str()) != 0) perror("Error renaming file"); else cout << "File renamed successfully";` It doenst work – Impact Nov 01 '20 at 23:13
  • not exist any "special characters" in file name. for rename file use `SetFileInformationByHandle` with `FileRenameInfo[Ex]` – RbMm Nov 01 '20 at 23:41
  • 1
    [There are lots of differences between DOS and cmd](https://superuser.com/a/1411173/241386). The commands you run on Windows aren't run under DOS – phuclv Nov 02 '20 at 00:03

1 Answers1

-1

Problem solved: Indeed the dir command cannot save utf8 correctly, i was forced to find another way. I took "dirent.h" from github it solved the problem in a few lines:

        DIR* directory = opendir(path);
        struct dirent* direntStruct;

        if (directory != NULL)
        {
            while (direntStruct = readdir(directory))
            {
                printf("File Name: %s\n", direntStruct->d_name);
            }
        }

However, it still can't handle characters like this: 'œ' Because this is really special it's about two letters in one. For example original name of:

"Breach - 160 - Testament - Ton cœur est ici.mkv"

Will be read as:

B4RHTQ~A.MKV

This is an internal problem in the dirent.h framework, anyway if you try to rename the file it still work ! Just don't expect getting information from the file name... I did not find a solution about this.

Impact
  • 31
  • 6
  • This answer will not help future readers with the same problem, since it does not say how to solve the problem. – JaMiT Nov 02 '20 at 23:21