Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SHFileOperation fails to delete files

417 views
Skip to first unread message

Eric Lilja

unread,
Nov 24, 2006, 4:29:17 PM11/24/06
to
Hi!
I want to delete the files in a given directory. I'm using the
following test function. It compiles cleanly, but when run a message
box pops up saying: Cannot delete file: Cannot read from the source
file or disk. I know the directory is valid. It contains the following
files:
Entries
Entries.Log
Repository
Root
Template
(the extension-less names are also files, not directories).

My test function simply performs:
SHFILEOPSTRUCT shfos;

ZeroMemory(&shfos, sizeof(shfos));
char buffer[1024] = {"C:\\emacs\\etc\\CVS\\\0"};

shfos.wFunc = FO_DELETE;
shfos.pFrom = buffer;
shfos.fFlags = FOF_ALLOWUNDO;

int ret = SHFileOperation(&shfos);

if (ret != 0)
MessageBox(NULL, "Error", "Error", MB_OK);

Any ideas?

/ E

Bertel Brander

unread,
Nov 24, 2006, 6:20:50 PM11/24/06
to
Eric Lilja skrev:

This one seems to do the trick nicely:
#include <windows.h>
#include <iostream>
#include <string.h>

int main()
{
SHFILEOPSTRUCT shfos;

memset(&shfos, 0, sizeof(shfos));
char buffer[1024] = {"C:\\T"};

shfos.wFunc = FO_DELETE;
shfos.pFrom = buffer;

shfos.fFlags = FOF_ALLOWUNDO | FOF_SILENT | FOF_NOCONFIRMATION;

int ret = SHFileOperation(&shfos);

if (ret != 0)
std::cout << "Error: " << GetLastError() << std::endl;
}

Are you sure that no one has links to the file in the folder
when you try to remove it?
Can you remove the folder by hand?

--
Just another homepage:
http://damb.dk
But it's mine - Bertel

Eric Lilja

unread,
Nov 24, 2006, 7:33:37 PM11/24/06
to

Bertel Brander skrev:

Yes, I can without. No problem there. Hmm.

malloc

unread,
Nov 28, 2006, 2:25:18 PM11/28/06
to
On 24 Nov 2006 13:29:17 -0800, "Eric Lilja" <mindc...@gmail.com>
wrote:

SHFILEOPSTRUCT fo;

char path[256] = "C:\\Folder\\:
char name[128] = "nameOfFile.wtf";
strcat ( path, name );

fo.hwnd = hMainWnd; //puts any messageboxes on top
fo.wFunc = FO_DELETE;
fo.fFlags = FOF_ALLOWUNDO;

fo.pFrom = path;
fo.pTo = "Recycle Bin";

SHFileOperation ( &fo );
if ( fAnyOperationsAborted == FALSE )
{ ResetList ( " or whatever " ); }

The pathname has to be fully qualified and (of course) correct.
Filenames taken from listboxes and textfiles have newline
markers attached to the end of the filename. \0 \n \r \t...
Try StrTrim ( name, " \0 " ); StrTrim ( name," \n ");
StrTrim ( name," \r "); StrTrim ( name, " " );

For me , SHFileOperation fails for2 reasons. Incorrect path and
filename and ' Read-Only' files.


iatethew...@gmail.com

unread,
Dec 9, 2006, 3:24:47 AM12/9/06
to
Ensure you paths for the pFrom and pTo buffers are double null
terminated using a debugger. Otherwise, SHFileOperation will hit the
first null, not see another one, and read into the rest of your buffer
memory for the next path and return the "path not found" error.

See the below blog entries:

http://shellrevealed.com/blogs/shellblog/archive/2006/09/11/Common-Questions-Concerning-the-SHFileOperation-API_3A00_-Part-1.aspx

http://shellrevealed.com/blogs/shellblog/archive/2006/09/28/Common-Questions-Concerning-the-SHFileOperation-API_3A00_-Part-2.aspx

Chris

0 new messages