//欢迎指正,转载请注明出处:https://blog.csdn.net/qq_42189368/article/details/80670210 ////代码:文件夹中限制文件数目,超出数目后,按照创建时间顺序,删除时间最早的文件 #include <io.h> #include <vector> #include <AtlBase.h> #include <time.h> #include <string.h> #include <iostream> #include <sstream> #include <fstream> #include <conio.h> using namespace std; int compare( char* time1, char* time2); BOOL CopyAndDel(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName); void GetAllFileInfo(LPCTSTR path, vector<LPCTSTR> &filesPathVector, vector <__time64_t> &timebuf) { //find the first file _tfinddata64_t c_file; intptr_t hFile; TCHAR root[MAX_PATH]; _tcscpy(root, path); _tcscat(root, _T("//*.*")); hFile = _tfindfirst64(root, &c_file); if (hFile == -1) return; __time64_t timecount = 0; TCHAR *oldPath = new TCHAR[MAX_PATH]; LPCTSTR movpath = _T("1.bmp"); //search all files recursively. do { if (_tcslen(c_file.name) == 1 && c_file.name[0] == _T('.') || _tcslen(c_file.name) == 2 && c_file.name[0] == _T('.') && c_file.name[1] == _T('.')) continue; TCHAR *fullPath = new TCHAR[MAX_PATH]; _tcscpy(fullPath, path); _tcscat(fullPath, _T("//")); _tcscat(fullPath, c_file.name); if (c_file.attrib&_A_SUBDIR) { GetAllFileInfo(fullPath, filesPathVector, timebuf); } else { //store full file path in vector. filesPathVector.push_back(fullPath); //print file info _tprintf(_T("FileName: %s\r\n"), fullPath); //_tprintf(_T("ReadOnly: %s\r\n"), // (c_file.attrib & _A_RDONLY) ? _T(" Y ") : _T(" N ")); //_tprintf(_T("Hidden: %s\r\n"), // (c_file.attrib & _A_HIDDEN) ? _T(" Y ") : _T(" N ")); //_tprintf(_T("System: %s\r\n"), // (c_file.attrib & _A_SYSTEM) ? _T(" Y ") : _T(" N ")); //_tprintf(_T("Arch: %s\r\n"), // (c_file.attrib & _A_ARCH) ? _T(" Y ") : _T(" N ")); TCHAR timeBuffer[30]; TCHAR *timebufo = new TCHAR[MAX_PATH]; _tctime64_s(timeBuffer, _countof(timeBuffer), &c_file.time_write); _tprintf(_T("WriteTime:%.24s\r\n\r\n"), timeBuffer); //保存时间最早的文件 // if (c_file.time_write <= timecount) // { // oldPath = fullPath; // timebuf.push_back(oldPath); // } timecount = timecount ? (c_file.time_write <= timecount ? c_file.time_write : timecount) : c_file.time_write; timebufo = timeBuffer; timebuf.push_back(timecount); } } while (_tfindnext64(hFile, &c_file) == 0); //close search handle _findclose(hFile); } void swap(int& a, int& b) { int c; c = a; a = b; b = c; } void sort(int* a, int n)//快排函数,从小到大 { if (n <= 1) return; if (n == 2){ if (a[1] < a[0]) swap(a[1], a[0]); return; } swap(a[0], a[n / 2]); int t = *a; int* L = a + 1;//从左向右走,遇到不小于分界值的停下 int* R = a + n - 1;//从右向左走,遇到小于分界值的停下 while (L < R){ while (L < R&&*L < t) ++L; while (a < R&&*R >= t) --R; if (L < R) swap(*L, *R); } swap(*a, *R); sort(a, R - a); sort(R + 1, n - (R - a) - 1); } int _tmain(int argc, _TCHAR* argv[]) { //读取参数配置文件 string copypath, IDpath, FeaMappath; int maxnum=30; IDpath="D:\\test"; string copyname; LPCTSTR movpath; LPCTSTR openIDpath; openIDpath = IDpath.c_str(); LPCTSTR openFeaMappath; openFeaMappath = FeaMappath.c_str(); while (true) { vector<LPCTSTR> filesPathVector; vector <__time64_t> timebuf; GetAllFileInfo(openIDpath, filesPathVector, timebuf); if (filesPathVector.size() > maxnum && timebuf.size()>0) { for (int j = timebuf.size(); j > 1;j--) { for (int i = j - 1; i > 0; i--) { __time64_t tempval; LPCTSTR tempname; //按照时间旧到新 if (timebuf.at(i) <= timebuf.at(i - 1)) { tempval = timebuf.at(i); timebuf.at(i) = timebuf.at(i - 1); timebuf.at(i - 1) = tempval; tempname = filesPathVector.at(i); filesPathVector.at(i) = filesPathVector.at(i - 1); filesPathVector.at(i - 1) = tempname; } } } int coutname = maxnum; for (int i = 0; i < filesPathVector.size(); i++, coutname--) { copyname = filesPathVector.at(i); int pos1 = copyname.find_last_of("//"); copyname = copypath+copyname.substr(pos1 + 1); movpath = copyname.c_str(); CopyFile(filesPathVector.at(i), movpath,TRUE); DeleteFile(filesPathVector.at(i)); if (coutname < (maxnum - 5)) { break; } } } filesPathVector.clear(); timebuf.clear(); GetAllFileInfo(openFeaMappath, filesPathVector, timebuf); if (filesPathVector.size() > maxnum && timebuf.size() > 0) { for (int j = timebuf.size(); j > 1; j--) { for (int i = j - 1; i > 0; i--) { __time64_t tempval; LPCTSTR tempname; //按照时间旧到新 if (timebuf.at(i) <= timebuf.at(i - 1)) { tempval = timebuf.at(i); timebuf.at(i) = timebuf.at(i - 1); timebuf.at(i - 1) = tempval; tempname = filesPathVector.at(i); filesPathVector.at(i) = filesPathVector.at(i - 1); filesPathVector.at(i - 1) = tempname; } } } int coutname = maxnum; for (int i = 0; i < filesPathVector.size(); i++, coutname--) { DeleteFile(filesPathVector.at(i)); if (coutname < (maxnum - 5)) { break; } } } } return 0; } int compare( char* time1, char* time2) { int year1, month1, day1, hour1, min1, sec1; int year2, month2, day2, hour2, min2, sec2; //printf("%s", &time1); //printf("%s", &time2); sscanf(time1, "%d-%d-%d %d:%d:%d", &year1, &month1, &day1, &hour1, &min1, &sec1); sscanf(time2, "%d-%d-%d %d:%d:%d", &year2, &month2, &day2, &hour2, &min2, &sec2); int tm1 = year1 * 10000 + month1 * 100 + day1; int tm2 = year2 * 10000 + month2 * 100 + day2; if (tm1 != tm2) return (tm1 > tm2) ? 1 : 0;//如果相等,大返回1,小返回0 tm1 = hour1 * 3600 + min1 * 60 + sec1; tm2 = hour2 * 3600 + min2 * 60 + sec2;//将时分秒转换为秒数 if (tm1 != tm2) return (tm1 > tm2) ? 1 : 0;//如果相等,大返回1,小返回0 return 2;//到这里必然是相等了 } BOOL CopyAndDel(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName) { if (CopyFile(lpExistingFileName, lpNewFileName, FALSE)) { if (!DeleteFile(lpExistingFileName)) { SetFileAttributes(lpExistingFileName, FILE_ATTRIBUTE_NORMAL); return DeleteFile(lpExistingFileName); } return TRUE; } return FALSE; }
C/C++按照文件属性对文件进行复制、移动、删除操作
猜你喜欢
转载自blog.csdn.net/qq_42189368/article/details/80670210
今日推荐
周排行