-
SHELL - SHFileOperation
Hello.
I wanted to copy a single file to another location, using shell functions. So I did this:
Code:
// ******** get path ****************
char path[_MAX_PATH];
SHGetPathFromIDList (
pidl,
path
);
// ******** file operation ************
SHFILEOPSTRUCT fopstruct;
fopstruct.hwnd = NULL; // parent wnd
fopstruct.wFunc = FO_COPY; // flag
fopstruct.pFrom = path; // path of the object user selects from browse for folder dialog box
fopstruct.pTo = "E:\cookies"; // destination
BOOL foCheck = SHFileOperation (
&fopstruct
);
I tried above code so many times but never worked. I don’t get any errors though, the file/folder just doesn’t get copied.
So I tried hard coding a path into pFrom, member of SHFILEOPSTRUCT:
Code:
fopstruct.pFrom = “E:\Debug”;
Then the file got copied properly into “E:\cookies”. But I think I cannot rely on hard coded paths.
Also, when I tried to delete a file/folder suing FO_DELETE flag, it didn’t worked either, even when I hard coded the path.
Why these things are happening? How can I fix them? Please help.
Thanks a lot for reading.
-geek@02
-
1. You need to make sure SHGetPathFromIDList() is succeeding.
2. You must zero out all windows structures before use. This avoids problems when you don't fill in all the members, as you are not in this case.
3. pFrom and pTo must be double null terminated.
4. Also make sure that SHBrowseForFolder() is succeeding.
Code:
char path[MAX_PATH + 1]; // Space provided for extra null terminator
if (SHGetPathFromIDList (pidl, path)) // Get path
{
// ******** file operation ************
path[lstrlen[path] + 1] = '\0'; // Double null terminate path
SHFILEOPSTRUCT fopstruct = { 0 }; // Zero out structure before use
fopstruct.hwnd = NULL; // parent wnd
fopstruct.wFunc = FO_COPY; // function
fopstruct.fFlags = 0; // flags
fopstruct.pFrom = path; // path of the object - double null terminated
fopstruct.pTo = "E:\\cookies\0"; // destination - double null terminated
BOOL foCheck = SHFileOperation (&fopstruct);
if (!foCheck) MessageBox(NULL, "SHFileOperation failed!", NULL, MB_ICONSTOP);
}
else
{
MessageBox(NULL, "SHGetPathFromIDList failed!", NULL, MB_ICONSTOP);
}
CoTaskMemFree(pidl); // Release pidl returned by SHBrowseForFolder
-
Don't forget to double up on backslashes in string literals as well.
You have "E:\cookies", where it should be as anonytmouse posted.
gg
-
Yes, now it’s working.
Thanks.