问题描述
我正在用 C++ 编写一个工具来删除 000.exe 恶意软件。该恶意软件在桌面上创建了许多名为“UR NEXT 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 Tool\n\nby OrcaTech\n\nThis 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,(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);
}
}