I am writing a small application to clean up the users computer (Temp Files, Internet History & Defrag) all in one. I have the UI implemented, however I have run in to some trouble when writing the code for scanning the Temp folder (C:\\test\\*.txt in this code for testing purposes). The code seems to work as normal on the surface, the progress bar increases etc. However, after running said code, the txt files in the directory still remained.
I've included the extracted 'Search and Destroy' code.
Is there an error in my code that I am overlooking?
This copies a pointer not a string. All of your 'filename' pointers point to the same buffer: file_data.cFileName. And that buffer becomes invalid after FindClose (I think).
Use strings:
1 2 3 4 5 6 7 8 9 10 11
// WCHAR *filename[500]; // boo
std::vector< std::wstring > filenames; // yay
//...
filename [num_files] = file_data.cFileName; // this will work as you expect now
//...
DeleteFile(filename[ftd].c_str()); // call c_str to get a pointer
Or.... you could just delete the file inside the while loop instead of having a second loop like that.
Also... All of this WinAPI stuff take TCHARs, not WCHARs. You should either switch to tchars, or switch to W versions of these structs/functions
I edited my code and everything worked like before. It seemed like it's doing something. There are no compiler or runtime errors and the progress bar increaseed but the files still remain undeleted. I checked out the MSDN reference on DeleteFile() [http://msdn.microsoft.com/en-us/library/aa363915%28VS.85%29.aspx] and included some error checking routines (Code Below) and got File Not Found for two instances.
The Files are definatley in the directory (Checked using Explorer & cmd just in case) and there are three files, not two. They all remain after the code is executed.
Apologies for being a nuicance and thanks for your help so far!