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]

# 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
# 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 >

# 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
# 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
# 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