SHFileOpertion and FO_DELETE

S

Simon Jefferies

Hello,

I am trying to delete a file using SHFileOperation, but get the error code
1026.

Code snippet:

SHFILEOPSTRUCT fo;
ZeroMemory(&fo, sizeof(fo));
fo.fFlags = FOF_ALLOWUNDO | FOF_FILESONLY;
fo.wFunc = FO_DELETE; // delete contents of this folder
fo.pFrom = szFilename;
int nSuccessCode = SHFileOperation(&fo);

The file does exist and have tried with other files/paths etc but these do
not work.

Any ideas?
TIA
Simon Jefferies
 
K

KC

There are a few reason why the operation failed. The file could be in use
and thus the deletion failed.

Another reason is, szFilename is not double NULL terminated. To ensure a
string is double NULL terminated you can initialize your buffer to NULL.

char szFileName[260];
memset(szFileName, 0, sizeof(szFileName));

strcpy(szFileName, "C:\\Hello");

Both member of SHFILEOPSTRUCT (pFrom and pTo) must be double NULL
terminated if they contain value.

Regards,
KC
 
D

Deepak

Hi Simon Jefferies
try this code it seems to work i tried this

SHFILEOPSTRUCT s ;
::ZeroMemory ( &s, sizeof ( s ) ) ; // Initialize the structure

CString f = "C:\\MyFolder" ;
//CString t = "D:\\" ;

char from [ MAX_PATH ], to [ MAX_PATH ] ; // buffer used to copy source and
target path

strcpy ( from, f ) ;
int l = f.GetLength( ) ;
from[l+1] = '\0' ;

//strcpy ( to, t ) ;
//l = t.GetLength( ) ;
//to[l+1] = '\0' ;

// Set the values in structure
s.hwnd = this -> m_hWnd ;
s.wFunc = FO_DELETE ;
s.pFrom = ( LPCTSTR ) from ;
//s.pTo = ( LPCTSTR ) to ;
s.fFlags = FOF_SIMPLEPROGRESS ;
s.lpszProgressTitle = "Moving files..." ;

::SHFileOperation ( &s ) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top