概述: 调用 Windows API 遍历、删除文件,涉及到的API接口:
deletefile、RemoveDirectory、MoveFileExA、
MoveFile
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <winbase.h>
#include <WinUser.h>
#include <strsafe.h>
#include <ShObjIdl.h>
#include <cstring>
#include <shlobj.h>
#include <comutil.h>
#include <io.h>
#pragma comment(lib, "comsuppw.lib")
#pragma comment(linker, "/entry:mainCRTStartup")
// 设置系统重启时删除
bool _SetFileRebootDelete(const std::string& file_path) {
if (MoveFileExA(file_path.c_str(), 0, MOVEFILE_DELAY_UNTIL_REBOOT)) {
return true;
}
else {
return false;
}
}
void getFiles(std::string path, std::vector<std::string>& files, std::vector<std::string>& names)
{
//文件句柄,win10用long long,win7用long就可以了
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
//如果是目录,迭代之 //如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
getFiles(p.assign(path).append("\\").append(fileinfo.name), files, names);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
names.push_back(fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
std::string GetLoactionDir()
{
DWORD dwLen = 0;
CHAR szAppDataDir[MAX_PATH];
dwLen = ExpandEnvironmentStringsA("%programdata%", szAppDataDir, MAX_PATH);
if (dwLen != 0) {
StringCchCatA(szAppDataDir, sizeof(szAppDataDir), ("\\TrustAgent\\logs"));
}
return szAppDataDir;
}
int GetWithRegex() {
// 定义文件查找句柄和查找条件
WIN32_FIND_DATAA findData;
HANDLE hFind = INVALID_HANDLE_VALUE;
LPCSTR lpPath = "D:\\Documents\\B_Tools\\"; // 查找路径,这里以C盘根目录为例
LPCSTR lpPattern = "*.7z"; // 查找模式,这里匹配所有文件
char buffer_1[MAX_PATH] = "";
char* lpStr1;
lpStr1 = buffer_1;
PathCombineA(lpStr1, lpPath, lpPattern);
// 调用FindFirstFile函数开始查找文件
hFind = FindFirstFileA(lpStr1, &findData);
if (hFind == INVALID_HANDLE_VALUE) {
std::cout << "FindFirstFile failed with error: " << GetLastError() << std::endl;
return 1;
}
// 循环遍历查找结果并输出文件名
do {
std::cout << findData.cFileName << std::endl;
} while (FindNextFileA(hFind, &findData));
// 关闭文件查找句柄
FindClose(hFind);
return 0;
}
int main()
{
using namespace std;
std::string filepath = GetLoactionDir();
vector<string> files;
vector<string> names;
getFiles(filepath, files, names);
for (int i = 0; i < files.size(); i++)
{
printf("-----========files:%d========---\n", i);
// cout << "files" << i << ':' << files[i] << endl;
// wstring wsName;
// wsName = string2wstring(files[i]);
cout << "lnkPath: " << files[i] << endl;
// wstring wsDestPath = getLnkFormPath(wsName);
// cout << "destPath:";
// wcout << wsDestPath;
// cout << endl;
_SetFileRebootDelete(files[i]);
}
return 0;
}
deletefile
#include <Windows.h>
int main()
{
LPCWSTR filePath = L"C:\\Path\\To\\File.txt";
if (DeleteFile(filePath))
{
// 文件删除成功
printf("文件删除成功!\n");
}
else
{
// 文件删除失败
printf("文件删除失败!\n");
}
return 0;
}RemoveDirectory
#include <Windows.h>
int main()
{
LPCWSTR directoryPath = L"C:\\Path\\To\\Directory";
if (RemoveDirectory(directoryPath))
{
// 目录删除成功
printf("目录删除成功!\n");
}
else
{
// 目录删除失败
printf("目录删除失败!\n");
}
return 0;
}补充
除了DeleteFile和RemoveDirectory之外,Windows API 还提供了其他一些用于删除文件或目录的函数。以下是其中一些常用的函数:
DeleteVolumeMountPoint: 该函数用于删除一个装载点的所有卷装信息。RemoveDirectoryEx: 该函数用于删除一个目录及其所有子目录和文件。RemoveFile: 该函数用于删除一个文件,但不会删除与该文件关联的目录。MoveFile: 该函数可以将一个文件或目录从一个位置移动到另一个位置,如果目标位置已经存在同名文件或目录,则会自动将其覆盖。MoveFileEx: 该函数可以将一个文件或目录从一个位置移动到另一个位置,同时可以指定不同的覆盖行为。SetFileAttributes: 该函数可以设置一个文件或目录的属性,例如只读、隐藏、系统等。DeleteFileTransacted: 该函数可以删除一个文件,并将其从回收站中清空。DeleteSubdirectoriesAndFiles: 该函数可以删除一个目录及其所有子目录和文件,并将其从回收站中清空。