简体   繁体   English

如何在 CMD/c++ 中重命名包含特殊字符的文件?

[英]How to rename file containing special characters in CMD/c++?

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:这是我尝试通过系统命令使用 dos 命令在 c++ 中执行的操作:

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.这是我通过 c++ 程序启动的 dos 命令,因此我可以使用更多本机函数。

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.所以我尝试了"wstring" ,我尝试了"#Define UNICODE""#Define _UNICODE" ......没有任何效果。

EDIT: I used cmd/dos because of the dir method which is usefull.编辑:我使用 cmd/dos 因为 dir 方法很有用。 I save the dir method like this:我像这样保存 dir 方法:

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);
                    }
              }

Problem solved: Indeed the dir command cannot save utf8 correctly, i was forced to find another way.问题已解决:dir 命令确实无法正确保存 utf8,我不得不另辟蹊径。 I took "dirent.h" from github it solved the problem in a few lines:我从 github 中获取了“dirent.h”,它用几行代码解决了这个问题:

        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 !这是 dirent.h 框架中的内部问题,无论如何,如果您尝试重命名文件,它仍然有效! Just don't expect getting information from the file name... I did not find a solution about this.只是不要指望从文件名中获取信息......我没有找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM