can not delete a file

Hello everyone,

I am using the following program to delete all files in a specified directory. But when running, no files could be deleted, and the related error information is,

failed with error 5 -- access denied. Anything wrong with the program?

remove_non_empty_directory ("c:\\temp\\non_empty_dir\\*");

// ErrorExit implementation from MSDN
// http://msdn2.microsoft.com/en-us/library/ms680582.aspx

int remove_non_empty_directory (const char* path)
{

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
int rtn;

hFind = FindFirstFile(path, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
FindClose(hFind);
return -1;
}
else
{
// delete 1st file
rtn = DeleteFile(&(FindFileData.cFileName));

if (0 == rtn)
{
ErrorExit (NULL);
}

// List all the other files in the directory and delete all files
while (FindNextFile(hFind, &FindFileData) != 0)
{
rtn = DeleteFile(&(FindFileData.cFileName));
}

FindClose(hFind);
}

return 0;

}

thanks in advance,
George
[1284 byte] By [George2] at [2007-11-20 9:42:24]
# 1 Re: can not delete a file
As the error suggests: Do you have enough privilegues to delete the file/s?
NoHero at 2007-11-9 13:31:32 >
# 2 Re: can not delete a file
Sure NoHero,

As the error suggests: Do you have enough privilegues to delete the file/s?

I have the privilege. I am admin and I can manually delete such files. I think there should be something wrong with my program, but through debugging for almost an hour, I still can not find the reason. Any ideas?

regards,
George
George2 at 2007-11-9 13:32:32 >
# 3 Re: can not delete a file
// delete 1st file
rtn = DeleteFile(&(FindFileData.cFileName));

You are already passing a pointer to the first element of an array to DeleteFile. No reason to take the pointer to the pointer... ;)

Try:

rtn = DeleteFile(FindFileData.cFileName);
NoHero at 2007-11-9 13:33:30 >
# 4 Re: can not delete a file
Adding to NoHero, Are you sure that your application has no open handles to those files ?
Krishnaa at 2007-11-9 13:34:34 >
# 5 Re: can not delete a file
You are right, NoHero!

// delete 1st file
rtn = DeleteFile(&(FindFileData.cFileName));

You are already passing a pointer to the first element of an array to DeleteFile. No reason to take the pointer to the pointer... ;)

Try:

rtn = DeleteFile(FindFileData.cFileName);

regards,
George
George2 at 2007-11-9 13:35:31 >
# 6 Re: can not delete a file
Thanks Krishnaa, no other applications have any handles to the files.

Adding to NoHero, Are you sure that your application has no open handles to those files ?

regards,
George
George2 at 2007-11-9 13:36:40 >
# 7 Re: can not delete a file
You are right, NoHero!

You are welcome ;)
But I have to admit, my eyes are getting sloppy :D
NoHero at 2007-11-9 13:37:43 >
# 8 Re: can not delete a file
Just aside note, it better using ::SHFileOperation() ( http://msdn2.microsoft.com/en-us/library/ms647743.aspx) since DeleteFile() will fail in deleting read only files.

Cheers
golanshahar at 2007-11-9 13:38:35 >
# 9 Re: can not delete a file
Thanks golanshahar,

Just aside note, it better using ::SHFileOperation() (http://msdn2.microsoft.com/en-us/library/ms647743.aspx) since DeleteFile() will fail in deleting read only files.

Cheers

SHFileOperation can not be used for Windows CE platform.

regards,
George
George2 at 2007-11-9 13:39:43 >