可视化检查文件名C++中是否有特定字符串



我正在用C++编程一个工具来删除000.exe恶意软件。该恶意软件在桌面上创建了许多名为"的文件;UR NEXT UR NEXT";等等。我的第一步是从桌面上删除所有这些文件。我能做些什么来检查桌面上的每个文件以及每个包含字符串"的文件;UR NEXT";在文件名的某个地方,删除它。我已经编写了程序的基本结构,但我真的很难弄清楚用户的用户名文件夹,然后删除任何包含"的文件;UR NEXT";在桌面上。如有任何帮助,我们将不胜感激。我正在使用Visual Studio 2019,我已经在程序中添加了一个提升。

#include <iostream>
#include <Windows.h>
#include <WinUser.h>
using namespace std;
int main()
{
string answer;
cout << "=~=~=~=~=~=~=~=~=~=~=~=~=~=n000.exe Removal Toolnnby OrcaTechnnThis tool can be used to remove the 000.exe malware from your Windows PC. Type "y" below and press [ENTER] to begin the removal process.n=~=~=~=~=~=~=~=~=~=~=~=~=~=" << endl;
cin >> answer;
if (answer == "y")
{
cout << "Starting Removal Process..." << endl;
cout << "Your computer will restart multiple times." << endl;
//Stop "run away" spam message boxes
system("taskkill /f /im runaway.exe");
//Change the wallpaper back to the default.
const wchar_t* path = L"%SystemRoot%\Web\Wallpaper\Windows\img0.jpg";
SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void*)path, SPIF_UPDATEINIFILE);
/* code to delete all files on desktop containing UR NEXT goes here */
system("pause");
return 0;
} else {
exit(0);
}
}

您可以使用std::filesystem::directory_iterator迭代桌面文件夹中的每个文件,并删除具有特定名称的文件:

#include <filesystem>
#include <string>
#include <vector>
int main()
{
std::vector<std::filesystem::path> filesToRemove;
for (const auto& i : std::filesystem::directory_iterator("path_to_desktop"))
{
std::string fileName = i.path().filename().string();
if (fileName.find("UR NEXT") != std::string::npos)
{
filesToRemove.emplace_back(i);
}
}
for (const auto& i : filesToRemove)
{
std::filesystem::remove(i);
}
}

最新更新