删除
system("rd /s/q C:\\Users\\Desktop\\kboard\\Line\\four1\\wrongLine");
//删除文件夹下的所有内容
void myDeleteDirectory(CString directory_path) //删除一个文件夹下的所有内容
{
CFileFind finder;
CString path;
path.Format(_T("%s/*.*"), directory_path);
BOOL bWorking = finder.FindFile(path);
//RemoveDirectory(finder.GetFilePath());
//DeleteFileW(finder.GetFilePath());
bool flag;
while (bWorking) {
bWorking = finder.FindNextFile();
if (finder.IsDirectory() && !finder.IsDots()) {//处理文件夹
myDeleteDirectory(finder.GetFilePath()); //递归删除文件夹
flag = RemoveDirectory(finder.GetFilePath());
}
else {//处理文件
DeleteFileW(finder.GetFilePath());
}
}
flag = RemoveDirectory(finder.GetFilePath());
}
//根据文件夹的命名删除,文件夹的命名格式年月日20230905
void deleteLogbyTime() {
SYSTEMTIME st = { 0 };
GetLocalTime(&st);
std::string path = "../logs";
std::vector<std::string> files;
GetAllFiles(path, files);
for (int i = 0; i < files.size(); i++) {
std::string fileName = files[i];
std::string::size_type pos = fileName.rfind("/");
std::string timeName = fileName.substr(pos + 1, 8);
try {
int time = atoi(timeName.c_str());
if (time < 100000) {
continue;
}
int year = time / 10000;
int month = (time - year * 10000) / 100;
int day = time % 100;
int nowtime = st.wYear * 10000 + st.wMonth * 100 + st.wDay;
if (nowtime - time > 30) {
CString pathcstr;
pathcstr = fileName.c_str();
myDeleteDirectory(pathcstr);
int res = _rmdir(fileName.c_str());//只能删除没有内容的文件夹只能删除空文件夹
}
}
catch (...) {
writeErrorLog("遇到非法命名的log文件" + fileName);
continue;
}
}
}
创建
void CreatDir(std::string dir)
{
int status = _access(dir.c_str(), 0);//判定文件夹是否存在,不存在则返回-1
if (status == -1)//判断存不存在
{
status = _mkdir(dir.c_str());
}
}
配置属性表QTVariant
string wrongfilePath = R"(C:\Users\Desktop\kboard\Line\four1\wrongLine)";
bool flag = CreateDirectory(wrongfilePath.c_str(), NULL);