Delete File Recursively not working.

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
32
33
34
35
36
37
38
39
40
#include<windows.h>
#include<string>
#include<iostream>
using namespace std;
int del()
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError;
    string FilePath = "C:\\test\\*.*";
    string FileName;
    hFind = FindFirstFile( FilePath.c_str(), &FindFileData);
    FileName = FindFileData.cFileName;
    cout<<"FOUND: "<<FindFileData.cFileName<<endl;
    
	while (FindNextFile(hFind, &FindFileData) != 0)
    {
    FileName = FindFileData.cFileName;
	cout<<"FOUND: "<<FileName<<endl;
	DeleteFile(FileName.c_str());//this does not work
	/*remove(FileName.c_str());//this doesn't work either*/

    }

    dwError = GetLastError();
    FindClose(hFind);
    if (dwError != ERROR_NO_MORE_FILES)
    {
    cout<<"ERROR: "<<dwError<<endl;
    return (-1);
    }
	return 0;
}

int main()
{

	del();
	return 0;
}

Its really annoying, what i am missing here..

EDIT: Never mind, i found out.. i forgot to put the whole adress in DeleteFile()
if anyone needs the same function he should use strcat() before DeleteFile().
Last edited on
You should use SHFileOperation() instead of your function, is already built-in and this is what windows explorer uses:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762164%28v=vs.85%29.aspx
Thanks for your suggestion, i will look into that some other day.
currently everything is working fine with my code except that it crashes after deleting the required files..
please take a look at my code
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<windows.h>
#include<string>
#include<iostream>
using namespace std;

int del(char * path)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError;
    string FilePath = "D:\\wamp\\*.*";
    string FileName;
    hFind = FindFirstFile( FilePath.c_str(), &FindFileData);
    FileName = FindFileData.cFileName;
    cout<<"FOUND: "<<FindFileData.cFileName<<endl;
    
	while (FindNextFile(hFind, &FindFileData) != 0)
    {
		FileName = FindFileData.cFileName;
		char *  file="";
		strcpy(file,path);
		strcat(file,FileName.c_str());
		cout<<"DELETED: "<<file<<endl;
		DeleteFile(file);
		
    }
	
    dwError = GetLastError();
	
    FindClose(hFind);
	
    if (dwError != ERROR_NO_MORE_FILES)
    {
		cout<<"ERROR: "<<dwError<<endl;
		return (-1);
    }
	
	return 0;
}

int main()
{
	del("D:\\wamp\\");
	return 0;
}
The problem is this:
1
2
3
4
5
6
7
8
9
10
while (FindNextFile(hFind, &FindFileData) != 0)
    {
		FileName = FindFileData.cFileName;
		char *  file="";
		strcpy(file,path);
		strcat(file,FileName.c_str());
		cout<<"DELETED: "<<file<<endl;
		DeleteFile(file);
		
    }


You are only allocate a pointer, but no memory in
file
variable. A quick fix could be this:
1
2
3
4
5
6
7
8
9
10
while (FindNextFile(hFind, &FindFileData) != 0)
    {
		FileName = FindFileData.cFileName;
		char  file [MAX_PATH] = {0};
		strcpy(file,path);
		strcat(file,FileName.c_str());
		cout<<"DELETED: "<<file<<endl;
		DeleteFile(file);
		
    }
Last edited on
Topic archived. No new replies allowed.