DeleteFile method seems not to work?

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?

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
progress_bar->Minimum = 0;
progress_bar->Maximum = num_files;
progress_bar->Step = 1;
WCHAR *filename[500];
WIN32_FIND_DATA file_data;
HANDLE search_handle;
search_handle = FindFirstFile(TEXT("C:\\test\\*.txt"), &file_data);
int num_files = 0;
while(FindNextFile(search_handle, &file_data))
{
filename [num_files] = file_data.cFileName;
num_files++;
}
FindClose(search_handle);
for(int ftd = 0;ftd < num_files;ftd++)
{
DeleteFile(filename[ftd]);
progress_bar->PerformStep();
}
pointers are not strings

filename [num_files] = file_data.cFileName;

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

See this: http://cplusplus.com/forum/articles/16820/
Last edited on
Aha! Thanks for the info. I did try using TCHARs at one point but my compiler threw errors at me and WCHARs seemed to work?

Thanks again
Actually I screwed up before

instead of this:

filename [num_files] = file_data.cFileName;

do this:

filename.push_back(file_data.cFileName);
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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
if(file_clean_check->CheckState == CheckState::Checked)
{
    if(temp_files_check->Checked)
   {
	 int num_files = 0;
	 std::vector< std::wstring > filenames;
	 WIN32_FIND_DATA file_data;
	 HANDLE search_handle;
	 search_handle = FindFirstFile(TEXT("C:\\test\\*.txt"), &file_data);
	 while(FindNextFile(search_handle, &file_data))
	 {
		 filenames.push_back(file_data.cFileName);
		 num_files++;
	 }
	 FindClose(search_handle);
	 progress_bar->Minimum = 0;
	 progress_bar->Maximum = num_files;
	 progress_bar->Step = 1;
	 for(int ftd = 0;ftd < num_files;ftd++)
        {
	    if(!DeleteFile(filenames[ftd].c_str()))
	    {
		    if(GetLastError() == ERROR_ACCESS_DENIED)
		    	MessageBox::Show("ACCESS DENIED","ACCESSERROR",MessageBoxButtons::OK);
		    if(GetLastError() == ERROR_FILE_NOT_FOUND)
			MessageBox::Show("FILE NOT FOUND","FNF",MessageBoxButtons::OK);
		    progress_bar->PerformStep();
            }
	}
   }
}
Topic archived. No new replies allowed.