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