标签:att warning efault first case roc std break st3
#pragma warning(disable : 4786) #include <iostream> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <fstream> #include <algorithm> #include <io.h> #include <windows.h> #include <process.h> using namespace std; #ifndef __FILEGETTER #define __FILEGETTER typedef unsigned int UINT32; typedef string::size_type UINTSIZE; const UINT32 FILETYPES = 7; const UINT32 SUFFIXLEN = 20; const char validFiles[FILETYPES][SUFFIXLEN] = {".c", ".cpp", ".h", ".hpp", ".java", ".docx", ".doc"}; class FileGetter { private: string srcDir; string destDir; vector<string>fileNames; bool isValidSrcFile(const string& fileName); void getFileNames(const string& filePath, bool childIncluded = false); const string getSuffix(const string& str); bool isDirectory(const string& path); public: FileGetter(const string& srcDir, const string& destDir); ~FileGetter(); void copyToDest(bool subDirInclude = false, bool isMove = false, int mode = 0); }; #endif
#include "FileGetter.h" #include <vector> #include <string> #include <cctype> #include <fstream> #include <algorithm> #include <io.h> using namespace std; FileGetter::FileGetter(const string& srcDir, const string& destDir) { string fileName(srcDir); do { string::size_type pos = fileName.find_last_of("\\"); if (string::npos != pos && pos + 1 == fileName.size()) { string tmp; tmp.assign(fileName.begin(), fileName.begin() + pos); fileName = tmp; } else { break; } } while (1); this->srcDir = fileName; this->destDir = destDir; } FileGetter::~FileGetter() { } bool FileGetter::isValidSrcFile(const string& fileName) { /*string::size_type fileNameLen = fileName.size(); for (UINT32 i = 0; i < FILETYPES; ++i) { if (fileNameLen > strlen(validFiles[i])) { string fileSuffix = fileName.substr(fileNameLen - strlen(validFiles[i]), fileNameLen); transform(fileSuffix.begin(), fileSuffix.end(), fileSuffix.begin(), ::tolower); if (0 == fileSuffix.compare(validFiles[i])) { return true; } } }*/ return true; } void FileGetter::getFileNames(const string& filePath, bool childIncluded) { //文件句柄 long hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; if((hFile = _findfirst(p.assign(filePath).append("\\*").c_str(),&fileinfo)) != -1) { do { //如果是目录,迭代之 //如果不是,加入列表 if (fileinfo.attrib & _A_SUBDIR) { if (childIncluded) { if ((0 != strcmp(fileinfo.name, ".")) && (0 != strcmp(fileinfo.name, ".."))) { getFileNames(p.assign(filePath).append("\\").append(fileinfo.name), childIncluded); } } } else { string fileName(fileinfo.name); if (isValidSrcFile(fileName)) { fileName = p.assign(filePath).append("\\").append(fileinfo.name); string::size_type pos = fileName.find("\\\\"); while (string::npos != pos) { string tmp; tmp.assign(fileName.begin(), fileName.begin() + pos + 1); tmp.append(fileName.begin() + pos + 2, fileName.end()); fileName = tmp; pos = fileName.find("\\\\"); } fileNames.push_back(fileName); } } } while(!_findnext(hFile, &fileinfo)); _findclose(hFile); } } const string FileGetter::getSuffix(const string& str) { int pos = str.find_last_of("."); string suffix; if ((string::npos != pos) && (pos + 1 != str.size())) { suffix.assign(str.begin() + pos + 1, str.end()); } return suffix; } bool FileGetter::isDirectory(const string& path) { struct _finddata_t fileinfo; string p; if (-1 != _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) { return ((fileinfo.attrib & _A_SUBDIR) == 0x01); } return false; } void FileGetter::copyToDest(bool subDirInclude, bool isMove, int mode) { getFileNames(srcDir, subDirInclude); string cmd; cmd.append("if not exist \"").append(destDir).append("\" (mkdir \"").append(destDir).append("\")"); cout << "aaa:" << cmd << endl; system(cmd.c_str()); for (int i = 0; i < fileNames.size(); ++i) { cout << "filename:" << fileNames[i] << endl; string tmpFileName; string subDir; int pos; switch(mode) { default: case 0://默认的按照原来的相对路径进行拷贝或移动 //tmpFileName.assign(fileNames[i].begin() + srcDir.size(), fileNames[i].end()); pos = fileNames[i].find_last_of("\\"); subDir = destDir; if (pos + 1 == srcDir.size()) { ; } else { subDir.append(fileNames[i].begin() + srcDir.size(), fileNames[i].begin() + pos + 1); } break; case 1://按照扩展名进行拷贝或移动 string suffix = getSuffix(fileNames[i]); subDir = destDir; subDir.append("\\").append(suffix); break; } if (subDir.length()) { cmd = ""; cmd.append("if not exist \"").append(subDir).append("\" (mkdir \"").append(subDir).append("\")"); cout << "xxxxx:" << cmd << endl; system(cmd.c_str()); } if (isMove) { cmd = ""; cmd.append("move \"").append(fileNames[i]).append("\" \"").append(subDir).append("\""); cout << "zzz:" << cmd << endl; system(cmd.c_str()); } else { cmd = ""; cmd.append("copy \"").append(fileNames[i]).append("\" \"").append(subDir).append("\"");; cout << "yyyy:" << cmd << endl; system(cmd.c_str()); } } } int main() { FileGetter fileGetter("c:\\test2", "c:\\test3"); fileGetter.copyToDest(1, 1); return 0; }
标签:att warning efault first case roc std break st3
原文地址:http://www.cnblogs.com/gardonkoo/p/7376704.html