// 文件和目錄操做 #ifndef _FILE_DIR_H_ #define _FILE_DIR_H_ #include <iostream> #include <vector> #include <string> void mkDir(const std::string &dirPath); int removeDir(const std::string &path); bool checkExist(const std::string &Path); int countFile(const std::string &dirPath, const std::string &filter); int countDir(const std::string &dirPath); void getAllFiles(const std::string &path, const std::string &filenameExtension, std::vector<std::string>& files); void getAllDirs(const std::string &path, std::vector<std::string>& dirs); void copyAllJpg(const std::string &sourcePath, const std::string &targetPath); void copyAllJpg_FromTxt(const std::string &txtPath, const std::string &targetPath, const std::string &newTxtPath); void copyAllModel(const std::string &sourcePath, const std::string &targetPath); void newCopyFile(const char* src, const char* target); #endif
#include "File_Dir.h" #include <fstream> #include <direct.h> #include <io.h> #include <windows.h> // 建立文件夾 void mkDir(const std::string &dirPath) { _mkdir(dirPath.c_str()); } // 重命名 // rename movefile... // 刪除該文件夾,包括其中全部的文件和文件夾 int removeDir(const std::string &path) { 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) { removeDir(p.assign(path).append("\\").append(fileinfo.name)); } } else { remove((p.assign(path).append("\\").append(fileinfo.name)).c_str()); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } return rmdir(path.c_str()); } // 檢查文件,目錄是否存在 bool checkExist(const std::string &Path) { if ((_access(Path.c_str(), 0)) != -1) { return true; } else { return false; } } // 計算dirPath路徑下filter(如:*.jpg)文件的數量 int countFile(const std::string &dirPath, const std::string &filter) { HANDLE hFind; WIN32_FIND_DATAA dataFind; BOOL bMoreFiles = TRUE; int iCount = 0; std::string temp = dirPath; temp += "\\"; temp += filter; hFind = FindFirstFileA(temp.c_str(), &dataFind); while (hFind != INVALID_HANDLE_VALUE && bMoreFiles == TRUE) { //判斷是不是文件 if (dataFind.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { iCount++; } bMoreFiles = FindNextFileA(hFind, &dataFind); } FindClose(hFind); return iCount; } // 計算dirPath路徑下文件夾的數量 int countDir(const std::string &dirPath) { int iCount = 0; long hFile = 0; struct _finddata_t fileinfo; std::string p; if ((hFile = _findfirst(p.assign(dirPath).append("\\*").c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { ++iCount; } } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } return iCount; } // 獲得path路徑下全部指定擴展名(如:.mp4)的文件名全稱 void getAllFiles(const std::string &path, const std::string &filenameExtension, std::vector<std::string>& files) { if (!checkExist(path)) return; 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) getAllFiles(p.assign(path).append("\\").append(fileinfo.name), filenameExtension, files); } else { if (p.assign(fileinfo.name).find(filenameExtension) != -1) files.push_back(p.assign(path).append("\\").append(fileinfo.name)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } void getAllDirs(const std::string &path, std::vector<std::string>& dirs) { if (!checkExist(path)) return; 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) { dirs.push_back(p.assign(path).append("\\").append(fileinfo.name)); getAllDirs(p.assign(path).append("\\").append(fileinfo.name), dirs); } } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } void copyAllJpg(const std::string &sourcePath, const std::string &targetPath) { std::vector<std::string> vec; int existNum = countFile(targetPath, "*.jpg"); char jpgCount_name[32]; getAllFiles(sourcePath, ".jpg", vec); for (std::string::size_type i = 0; i < vec.size(); i++) { ++existNum; _itoa(existNum, jpgCount_name, 10); std::string saveJpgPath = targetPath; saveJpgPath += "\\"; saveJpgPath += jpgCount_name; saveJpgPath += ".jpg"; // false: 覆蓋目標 CopyFileA(vec[i].c_str(), saveJpgPath.c_str(), false); } } // 讀取複製 txt 中圖片,同時在目標目錄下追加新的 txt void copyAllJpg_FromTxt(const std::string &txtPath, const std::string &targetPath, const std::string &newTxtPath) { std::vector<std::string> Jpg_List; std::ifstream if1(txtPath); while (!if1.eof()) { std::string s; getline(if1, s); if (s == "") continue; Jpg_List.push_back(s); if (if1.eof()) break; } if1.close(); if1.clear(); int existNum = countFile(targetPath, "*.jpg"); char jpgCount_name[32]; std::ofstream oftxt(newTxtPath, std::ios::app); if (!oftxt) return; for (std::string::size_type i = 0; i < Jpg_List.size(); i++) { ++existNum; _itoa(existNum, jpgCount_name, 10); std::string jpgName; jpgName.assign(jpgCount_name); if (jpgName.length() < 10) { jpgName.insert(0, 10 - jpgName.length(), '0'); } std::string saveJpgPath = targetPath; saveJpgPath += "\\"; saveJpgPath += jpgName; saveJpgPath += ".jpg"; // false: 覆蓋目標 CopyFileA(Jpg_List[i].c_str(), saveJpgPath.c_str(), false); oftxt << saveJpgPath << std::endl; } oftxt.close(); } void copyAllModel(const std::string &sourcePath, const std::string &targetPath) { std::vector<std::string> vec; int existNum = countFile(targetPath, "*.model"); char jpgCount_name[32]; getAllFiles(sourcePath, ".model", vec); for (std::string::size_type i = 0; i < vec.size(); i++) { ++existNum; _itoa(existNum, jpgCount_name, 10); std::string saveJpgPath = targetPath; saveJpgPath += "\\"; saveJpgPath += jpgCount_name; saveJpgPath += ".model"; // false: 覆蓋目標 CopyFileA(vec[i].c_str(), saveJpgPath.c_str(), false); } } void newCopyFile(const char* src, const char* target) { CopyFileA(src, target, false); }