开发者

SHFileOperation doesn't move all of a folder's contents

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜