c++ - SHFileOperation 不会 move 文件夹的所有内容

标签 c++ file directory move shfileoperation

这会在桌面上创建一个新文件夹,但不会将文件夹 .pfrom 的内容 move 到文件夹 .pTo。

int main()
{

    SHFILEOPSTRUCT sf = {0};
    TCHAR myt[MAX_PATH];
    GetModuleFileName(NULL, myt, MAX_PATH); // puts the currente exe path in the buffer myt
    string currentexepath;
    int i;

    for(i = 0; myt[i] != NULL; i++) {  // this loop is for converting myt to string
        currentexepath += myt[i];      // because string capabilities are needed
    }

    i = currentexepath.find_last_of("\\/");
    currentexepath = currentexepath.substr(0, i);
    currentexepath += "\\subfolder\\*.*\0"; //i tried with and without *.* and \0
    wstring ws = s2ws(currentexepath);

    sf.wFunc = FO_COPY;
    sf.hwnd = 0;
    sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
    sf.pFrom = ws.c_str();
    sf.pTo = L"C:\\Users\\Me\\Desktop\\folder\0";
    SHFileOperation(&sf);
}

// the following is from msdn
// http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375
wstring s2ws(const string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

最佳答案

SHFileOperation 需要一个双空终止字符串。但是你不能为此使用 std::string 或 std::wstring 。另见 Double null-terminated string .

当你这样做时:

currentexepath += "\\subfolder\\*.*\0";

字符串的 + 运算符看不到第二个 null 终止符,因为它在第一个 null 处停止。

这里有一个方法可以解决这个问题:

int main()
{
    SHFILEOPSTRUCT sf = {0};
    TCHAR myt[MAX_PATH];
    GetModuleFileName(NULL, myt, MAX_PATH); // puts the currente exe path in the buffer myt
    string currentexepath;

    if(TCHAR* LastSlash = _tcsrchr(myt, _T('\\'))) {
        *LastSlash = _T('\0');
    }

    // the pipe sign will be replaced with a \0 to get double null termination
    // because _tcscat_s and all other strcat functions stop at the first \0
    // we have to use this workaround
    _tcscat_s(myt, _T("\\subfolder\\*.*|")); 
    while (TCHAR* ptr = _tcsrchr(myt, _T('|'))) {
        *ptr = _T('\0'); 
    }

    sf.wFunc = FO_COPY;
    sf.hwnd = 0;
    sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
    sf.pFrom = myt;
    sf.pTo = L"C:\\Users\\wh\\Desktop\\folder\0";
    if(SHFileOperation(&sf)!=0) {
        // error occured
        MessageBox(NULL, L"SHFileOperation failed", L"Error", MB_OK);
    }
}

if() 和 while() 语句如何转换为 bool 值?

例如这个 if 语句:

    if(TCHAR* LastSlash = _tcsrchr(myt, _T('\\'))) {
        *LastSlash = _T('\0');
    }

也可以这样写:

    TCHAR* LastSlash = _tcsrchr(myt, _T('\\'));
    if(LastSlash) {
        *LastSlash = _T('\0');
    }

或:

    TCHAR* LastSlash = _tcsrchr(myt, _T('\\'));
    if(LastSlash != NULL) {
        *LastSlash = _T('\0');
    }

我将 TCHAR* 的赋值和检查结合在一条语句中。当指针转换为 bool 值时,NULL 变为假,所有其他值变为真。

关于c++ - SHFileOperation 不会 move 文件夹的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768403/

相关文章:

c++ - 在 C++08 中初始化对象以作为参数传递的首选语法是什么?

c - 如何将重定向输出写入 C 中循环内的文件?

c++ - 此代码是否通用,还是仅适用于我的系统?

C++ 标识符

ruby-on-rails - javascript MVC 和 Rails 文件夹结构

bash - 如何改进读取变量值并将其写入文件的逻辑?

c++ - C++ 中目录文件名的自然排序

matlab - 如何在 MATLAB/Octave 中将相对目录路径解析为规范路径

java - 如何使用 FTP 在 Java 中的一次调用中创建具有多个级别的目录

c++ - 如何使用 JNI 在 C++ 中加载 .jar 文件