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