|
-
July 30th, 2007, 05:11 AM
#1
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?
Code:
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
-
July 30th, 2007, 05:29 AM
#2
Re: can not delete a file
As the error suggests: Do you have enough privilegues to delete the file/s?
-
July 30th, 2007, 05:33 AM
#3
Re: can not delete a file
Sure NoHero,
 Originally Posted by 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
-
July 30th, 2007, 06:51 AM
#4
Re: can not delete a file
Code:
// 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:
Code:
rtn = DeleteFile(FindFileData.cFileName);
-
July 30th, 2007, 06:54 AM
#5
Re: can not delete a file
Adding to NoHero, Are you sure that your application has no open handles to those files ?
Regards,
Ramkrishna Pawar
-
July 30th, 2007, 07:45 AM
#6
Re: can not delete a file
You are right, NoHero!
 Originally Posted by NoHero
Code:
// 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:
Code:
rtn = DeleteFile(FindFileData.cFileName);
regards,
George
-
July 30th, 2007, 07:50 AM
#7
Re: can not delete a file
Thanks Krishnaa, no other applications have any handles to the files.
 Originally Posted by Krishnaa
Adding to NoHero, Are you sure that your application has no open handles to those files ?
regards,
George
-
July 30th, 2007, 08:06 AM
#8
Re: can not delete a file
 Originally Posted by George2
You are right, NoHero!
You are welcome 
But I have to admit, my eyes are getting sloppy
-
July 30th, 2007, 11:16 AM
#9
Re: can not delete a file
Just aside note, it better using ::SHFileOperation() since DeleteFile() will fail in deleting read only files.
Cheers
-
July 30th, 2007, 11:59 PM
#10
Re: can not delete a file
Thanks golanshahar,
 Originally Posted by golanshahar
Just aside note, it better using ::SHFileOperation() since DeleteFile() will fail in deleting read only files.
Cheers
SHFileOperation can not be used for Windows CE platform.
regards,
George
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|