|
It doesn't work in WinCE . And code in the article does.
|
|
|
|
|
There may be some errors in my code. Try it on your own. I just write it. It may contain errors. But I am always using this in my apps. As I said, it is faster, more reliable and easier
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
Mustafa Demirhan wrote:
op.pFrom = "c:\\yourfolder\\*.*";
I have not tested this, but I would suggest to write the pattern like '*', not '*.*'.
With the later, survivance of the Dos days, you risk to miss the files without extensions (like Makefile) or without name (like .unixhidden).
Now, I can be wrong
I was a bit skeptical about the portability of your snippet, but beside the mentioned CE problem, it seems to work on all versions of Win32. Wow.
Thank you, I added this to my collection of tips and tricks.
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
|
|
|
|
|
Philippe Lhoste wrote:
I have not tested this, but I would suggest to write the pattern like '*', not '*.*'.
With the later, survivance of the Dos days, you risk to miss the files without extensions (like Makefile) or without name (like .unixhidden).
...and I added this to my collection of tips and tricks Thank you.
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
I'm just trying this SHFileOperation snippet on a mapped network drive. It dosen't seem to work in this situation.
D'Oh
|
|
|
|
|
This piece of code is great. However, I find problem using it in COM DLL.
I try to use it in a COM DLL that I'm working on. But, I do not know what to put for op.hwnd? I tried to set op.hwnd = NULL, but SHFileOperation(&op) return me an int 87, and the operation failed. There was no explaination on return values in MSDN.
What should I put in op.hwnd? Any idea?
|
|
|
|
|
Put your window's handle. If you dont have any windows, I have no idea. In fact, I thought NULL should work.
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
Put the Desktop Window Handle by calling GetDesktopWindow()
I try and this is not the problem! I am not able to make work this piece of code!
Eric
Eric Forget
Windows Software Developer
|
|
|
|
|
The drawback with this is that you have no callback facility to allow the operation to be cancelled from within your app, especially if you said no progress dialog or do operation silently.
The original article allows that as you have more control to modify the code and therefore, I think, is more flexible and could be a more preferable solution depending on your requirements.
|
|
|
|
|
Try to delete 1000 files with that. It will take an hour to finish!
Also, if you put the progress dialogs, you would have the ability to cancel it in the middle (if you are fast enough)...
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
Sure, but I dont want a bloomin great progress dialog within my app. My app already has its own output window and a stop button therefore using this facility does not fit in with my UI.
This is why I suggested that it all depends on your requirements. THE SHFileOp is not currently preferable for my requirements, unless it has some way (like CopyFileEx) of passing in a pointer to a bool that allows the operation to be cancelled externally -- it would be perfect if it had that.
|
|
|
|
|
In using the SHFileOperation I am having trouble using it in a function. ie)
void CDlg::DeleteFiles(LPCSTR sFiles, ...)
{
SHFILEOPSTRUCT op;
op.hwnd = AfxGetApp()->GetMainWnd()->m_hWnd;
op.pFrom = sFiles;
op.pTo = "";
op.wFunc = FO_DELETE;
op.fFlags = 0;
...
SHFileOperation(&op);
}
I originally used CString in place of LPCSTR, but had the same results. However, at least with this method I can pass in a quoted string. ie) "c:\\data\\test.dat"
I have tried numerous string conversions in case it was a UNICODE issue. But, I always get the same result. (cannot delete file: cannot read from source file or disk).
I am currently using just the DeleteFile() function.
Also, does SHFileOperstion work with files in shared folders using UNC???
Thanks
|
|
|
|
|
|
I also had this error, because I forgot to
append one extra null at the end of pFrom.
(must be two nulls! RTFM =] )
Maybe you're doing the same mistake?
regards,
/ AA
|
|
|
|
|
Hi
I didnt get it what you said?This is deleting files if I specify path in double quotes like "C:\\my folder\\*.*" but not able to delete if I specify the path using CString or something like
CString strFolder="C:\\my folder\\*.*".
What should I do to solve this problem?
Thanks
Pathi
Pathi
|
|
|
|
|
Double NULLS! This way:
CString strFolder="C:\\my folder\\*.*\0".
/ A A
|
|
|
|
|
The documentation states that the hwnd parameter is:
"Window handle to the dialog box to display information about the status of the file operation"
Apparently if you don't like the default progress dialog you can use your own, although I have been unbale to find any documentation on how to do this.
You can recieve callbacks by implementing ICopyHook.
All in all SHFileOperation is much easier to use. It not only allows easy copy/move/delete but it also automaticlly displays replace and delete confirmation dialogs in whatever language OS the user has installed. If your planning on interacting with the user this is definitely the way to go.
There are a few tricks to using it though:
1) Always zero the SHFILEOPSTRUCT struct before using.
2) Make double sure your last string is double NULL terminated.
3) Read the docs
To make all this even easier have a look at Michael Dunn's excellent article:
http://www.codeproject.com/shell/cshellfileop.asp
Abraxas
|
|
|
|
|
Hello !
Thanks for that useful piece of code, I've just tried it and I have something very interesting...
The following function works great, however, if you comment the line op.pFrom = pepa; and
uncomment op.pFrom = temp.c_str(); it doesn't work anymore..
Strange since both strings are exactly the same (you can printf pepa and temp.. they're the same)
Is there something I am missing with the basic_string::c_str() ?
Thanks in advance!
int deleteDir(const char* directory)
{
SHFILEOPSTRUCT op;
string temp;
char pepa[512];
strnset(pepa, 0, 512);
strncpy(pepa, directory, 512);
strncat(pepa, "\\*.*", 512);
temp = directory;
temp += "\\*.*";
printf("deleting : [%s]\n", temp.c_str());
op.hwnd = NULL;
//op.pFrom = temp.c_str();
op.pFrom = pepa;
op.pTo = "";
op.wFunc = FO_DELETE;
op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
SHFileOperation(&op);
printf("%d\n", GetLastError());
RemoveDirectory(directory);
return 0;
}
|
|
|
|
|
I had a similar problem with CStrings. I had this code and I wanted it to work:
------------ BROKEN CODE BELOW: ------------
SHFILEOPSTRUCT op;
op.hwnd = AfxGetApp()->GetMainWnd()->m_hWnd;
op.pFrom = szFilePath.GetBuffer(1);
op.pTo = "";
op.wFunc = FO_DELETE;
op.fFlags = 0;
SHFileOperation(&op);
SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH, (LPCVOID) szLocalPath.GetBuffer(1), 0);
---------------------------------------------
The problem had to do with the way the characters are stored (double bytes). See (http://www.codeguru.com/Cpp/W-P/files/shellapifunctions/article.php/c1293/). So, I had to convert it to a usable manner. I did this and it is ugly, but it works. Maybe someone else has a better conversion method....
------------ WORKING CODE BELOW: ------------
char aChars[MAX_PATH];
memset(aChars, '\0', MAX_PATH);
strcpy(aChars, szFilePath.GetBuffer(1));
SHFILEOPSTRUCT op;
op.hwnd = AfxGetApp()->GetMainWnd()->m_hWnd;
op.pFrom = aChars;
op.pTo = "";
op.wFunc = FO_DELETE;
op.fFlags = 0;
SHFileOperation(&op);
SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH, (LPCVOID) szLocalPath.GetBuffer(1), 0);
----------------------------------------------
Hope this helps!
|
|
|
|
|
Message Closed
modified 12-Apr-16 5:29am.
|
|
|
|