Need to delete the files in a folder
I'm going insane. I cannot figure out what function to call to delete the files in a folder.
Code:
const wchar_t g_wsz_DriverBackupFolder[] = L"C:\\Sample\\*\0\0\0\0";
SHFILEOPSTRUCT info;
memset(&info, 0, sizeof(info));
info.wFunc = FO_DELETE;
info.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;
info.pFrom = g_wsz_DriverBackupFolder;
if (0 != SHFileOperation(&info))
{
DWORD dwError = GetLastError();
DWORD dwError1 = GetLastError();
DWORD dwError2 = GetLastError();
DWORD dwError3 = GetLastError();
}
All I get is an error: GetLastError = 6: "Invalid Handle". Oh thank yo that's useful.
I don't want any nag screens or anything asking me how I feel. I just want the contents to be gone. I also have no patience at this point whatsoever so please don't answer with a riddle or broken code fragment. I will just blow my top and since I'm not videoing this I won't even be able to post to YouTube whatever funny destructive thing I end up doing.
Re: Need to delete the files in a folder
Why don't you use _unlink to delete a file ?
Re: Need to delete the files in a folder
Quote:
Originally Posted by Skizmo
Why don't you use
_unlink to delete a file ?
Because I'm trying to delete all files from the folder.
Re: Need to delete the files in a folder
You can use CFileFind.
Code:
CFileFind clFiles;
clFiles.FindFile (_T("c:\\*.*"));
while (clFiles.FindNextFile ())
{
_wunlink (clFiles.GetFilePath ());
}
Re: Need to delete the files in a folder
Don't dare to try this myself :) but maybe you should provide a valid hwnd even though you have set FOF_SILENT.
Re: Need to delete the files in a folder
Quote:
Originally Posted by Skizmo
You can use CFileFind.
Code:
CFileFind clFiles;
clFiles.FindFile (_T("c:\\*.*"));
while (clFiles.FindNextFile ())
{
_wunlink (clFiles.GetFilePath ());
}
Can't use it. This is a plain and simple console app that does not use MFC.
Re: Need to delete the files in a folder
Quote:
Originally Posted by S_M_A
Don't dare to try this myself :) but maybe you should provide a valid hwnd even though you have set FOF_SILENT.
I have no window because this is just a plain and simple console app.
Re: Need to delete the files in a folder
You seem to have not read the document. If you did , you wouldn't have relied on GetLastError
Re: Need to delete the files in a folder
Well, here's what the problem was.
First, the path must be only the path (with the double NULL termination). No wildcards:
const wchar_t g_wsz_DriverBackupFolder[] = L"C:\\Sample\0\0";
Next, the FOF_FILESONLY flag must be specified.
So basically even though I already told it plenty of information for performing these tasks it required a tweak to a string and a special flag that the documentation certainly doesn't imply is required ("Files only" means that without it I'd risk killing directories. It should still DELETE!)
Naturally the Microsoft gurus behind the MSDN documentation didn't bother to document any of this and I had to get the answer online from people like you all.
Oh, and the function still claims to fail even though it succeeds. Good ol' Microsoft.
Re: Need to delete the files in a folder
Just a guess - but the error you're seeing might be because the file was a system file or something like that. I routinely 'normalize' the attributes of a file before attempting to delete it. Something like:-
Code:
::SetFileAttributes("path_to_file", FILE_ATTRIBUTE_NORMAL);
::remove("path_to_file");
Re: Need to delete the files in a folder
Quote:
Originally Posted by SteveS
Can't use it. This is a plain and simple console app that does not use MFC.
There's a Win32 equivalent of those functions which doesn't use MFC, FYI.