SHFileOperation 不会移动文件夹的所有内容

发布于 2024-11-03 08:30:47 字数 1339 浏览 0 评论 0原文

这会在桌面上创建一个新文件夹,但不会将文件夹 .pfrom 的内容移至文件夹 .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;
}

this makes a new folder on the desktop, but it doesn't move the contents of the folder .pfrom to the folder .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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2024-11-10 08:30:47

SHFileOperation 需要一个以双 n​​ull 结尾的字符串。但您不能为此使用 std::string 或 std::wstring 。另请参阅双 null 终止字符串

当您这样做时:

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() 语句如何转换为布尔值?

例如这个 if 语句:

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

也可以写成:

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

or:

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

我组合了赋值TCHAR* 和单个语句中的检查。当指针转换为布尔值时,NULL 变为 false,所有其他值变为 true。

SHFileOperation requires a double null terminated string. But you can't use std::string or std::wstring for that. See also Double null-terminated string.

When you do:

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

The + operator of the string does not see the second null-termination, because it stops at the first null.

Here is a way you can solve this:

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

how are the if() and while() statements converted to a boolean?

For example this if statement:

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

Can also be written like:

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

or:

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

I combined the assignment of the TCHAR* and the check in a single statement. When a pointer is converted to a boolean, then NULL becomes false, and all other values become true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文