FindFirstFile Win32 Function Can't Get It To Work Right?

So I Have some classes for deleting files and I wanted a function within those classes that would delete a file if the file name contains certain strings like for example "deleteme". That said here's some code and then I'll explain two problems I'm having: I really appreciate your guys and gals help so thanks in advance and more thanks soon::here we go

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


class DeleteMaliciousFiles
{
protected:
	string Directory, strSearch;
public:
	void set_directory(string a)
	{Directory = a;}

	void set_Searchstr(string a)
	{strSearch = a;}

	virtual bool DeleteSearch()
	{return (0);}

	bool NormalDelete(const char* thefile)
	{
		return (remove(thefile));
	}
};

class DeleteBySearch: public DeleteMaliciousFiles
{
public:
	bool DeleteSearch()
	{
		string strFile;
		HANDLE hFind;
		WIN32_FIND_DATAA FileFind;
		hFind = FindFirstFileA(Directory.c_str(), &FileFind);

		if(hFind != INVALID_HANDLE_VALUE)
		{
			strFile = FileFind.cFileName;
			if (strFile.find(strSearch) != string::npos)
			{
				FindClose(hFind);
				return (this->NormalDelete(strFile.c_str()));
			}
			while (FindNextFileA(hFind, &FileFind) != 0)
			{
				strFile = FileFind.cFileName;
				if (strFile.find(strSearch) != string::npos)
				{
					FindClose(hFind);
					return (this->NormalDelete(strFile.c_str()));
				}

			}
			return 0;
		}	
		else
		{
			return 0;
		}	
	}
	
};




Pretty explanatory my biggest issue which is driving me crazy say I use the searchdelete func like so


1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char *argv[])
{

	DeleteBySearch one;
	DeleteMaliciousFiles *mfile = &one;
	mfile->set_directory("*.txt");
	mfile->set_Searchstr("23");
	mfile->DeleteSearch();




If there is a text file in the same directory as the .exe is running IT WILL DELETE it if it contains the number 23 (So The code Works Fine) But now let's say I want to delete a file using that string argument and it's in another directory like C:\Users\main\Desktop and you want to delete a file if it contains 23? You would think you would do something like

1
2
3
4
5
6
7
8
int main(int argc, char *argv[])
{

	DeleteBySearch one;
	DeleteMaliciousFiles *mfile = &one;
	mfile->set_directory("C:\\Users\\main\\Desktop\\*.txt");
	mfile->set_Searchstr("23");
	mfile->DeleteSearch();

But I've tried that and I've tried *.txt* and Desktop\\* and NOTHING seems to work. Now why not? Driving me nuts.

Second issue For my normal delete method in the main class

1
2
3
4
bool NormalDelete(const char* thefile)
	{
		return (remove(thefile));
	}


That looks neater code wise then to just use the normal remove( function but if that's the only issue I guess I will just use remove( as it IS LESS CODE:: but just because I'm curious it says forcing value to bool 'true' or 'false' (performance warning). It compiles and all that jazz but it says there's "performance issues"

now whenever I hear that I think of the scene in Bad Santa? You know? He's like "So Don't let this inhibit your performance in any way". "Performance Like Sexual? "Are You referring To My Gear"?

Ok enough of that. Can someone explain why that would inhibit my "performance" that's the last thing I want, and then most of all why that FindFirstFileA function won't work when trying to go beyond the directory the .exe main file is in.

Thanks
closed account (DSLq5Di1)
Include the full path name in your call to NormalDelete(), else you are simply deleting from the current working directory (which is most likely your project folder).

The performance warning is no longer an issue, but it never hurts to be explicit. return remove(thefile) == 0;
http://stackoverflow.com/questions/206564/what-is-the-performance-implication-of-converting-to-bool-in-c

Also, stick with the Windows File API instead of mixing in calls to <cstdio>. http://msdn.microsoft.com/en-us/library/windows/desktop/aa363915
Sorry Sloppy9 sometimes my "performance" is off and I'm an idiot. That said I fixed the main problem and I'll use juts Win32 Thanks for pointing that out.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class DeleteMaliciousFiles
{
protected:
	string strDirectory, strSearch, strEnding;
public:
	void set_directory(string a)
	{strDirectory = a;}

	void set_Searchstr(string a)
	{strSearch = a;}

	void set_endingstr(string a)
	{strEnding = a;}

	virtual bool DeleteSearch()
	{return (0);}

	bool NormalDelete(const char* thefile)
	{
		return (remove(thefile));
	}
};

class DeleteBySearch: public DeleteMaliciousFiles
{
public:
	bool DeleteSearch()
	{
		string strInitialSearch = strDirectory+strEnding;
		string strFile;
		string strFileToDelete;
		HANDLE hFind;
		WIN32_FIND_DATAA FileFind;
		hFind = FindFirstFileA(strInitialSearch.c_str(), &FileFind);

		if(hFind != INVALID_HANDLE_VALUE)
		{
			strFile = FileFind.cFileName;
			if (strFile.find(strSearch) != string::npos)
			{
				strFileToDelete = strDirectory+strFile;
				FindClose(hFind);
				return (this->NormalDelete(strFileToDelete.c_str()));
			}
			while (FindNextFileA(hFind, &FileFind) != 0)
			{
				strFile = FileFind.cFileName;
				if (strFile.find(strSearch) != string::npos)
				{
					strFileToDelete = strDirectory+strFile;
					FindClose(hFind);
					return (this->NormalDelete(strFileToDelete.c_str()));
				}

			}
			return 0;
		}	
		else
		{
			return 0;
		}	
	}
	
};

int main(int argc, char *argv[])
{

	DeleteBySearch one;
	DeleteMaliciousFiles *mfile = &one;
	mfile->set_directory("C:\\Users\\main\\Desktop\\ha\\");
	mfile->set_endingstr("*.txt");
	mfile->set_Searchstr("23");
	mfile->DeleteSearch();


Works fine for a txt file that contains 23 in that directory will be deleted and the others left alone. Perfecto. Thanks for your help dude.
Topic archived. No new replies allowed.