#include #include #include #include ////判断目录或文件是否存在bool IsFileDirExist(string path){ if(_access(path.c_str(),0)!=-1) { //cout<<path.c_str()<<" exist!"<<endl; return true; } else return false;}
//说明://[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;// 如: DeleteFile("F:/here/file.txt");等同于DeleteFile("F://here//file.txt");//[3] 该函数删除指定目录下的所有文件,不递归删除子文件夹的内容bool DeleteFileInDir(string rootDir){ if (strcmp(rootDir.c_str(),"")==0 ||strcmp(rootDir.c_str(),"//")==0) { cout<<"非法路径!"<<endl; return false; } WIN32_FIND_DATA fd; ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); HANDLE hSearch; string filePathName; string tmpPath; filePathName=rootDir; BOOL bSearchFinished = FALSE; if( filePathName.data()[filePathName.length()-1] != '//' ) { filePathName+="//"; } filePathName+="*"; hSearch = FindFirstFile(filePathName.c_str(), &fd);
if (hSearch==INVALID_HANDLE_VALUE) { cout<<"Directory or file name is invalid!"<<endl; return false; } while( !bSearchFinished ) { if( FindNextFile(hSearch, &fd) ) { if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; tmpPath+=fd.cFileName; cout<<tmpPath.c_str()<< " is a directory! Not deleted."<<endl; } else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; if (tmpPath.data()[tmpPath.length()-1]!='//') { tmpPath+="//"; } tmpPath+=fd.cFileName; if (hSearch==INVALID_HANDLE_VALUE) { cout<<"Directory or file name is invalid!"<<endl; return false; } if(DeleteFile(tmpPath.c_str())) { cout<<"File "<<tmpPath.c_str()<<" is deleted!"<<endl; } else { cout<<"File "<<tmpPath.c_str()<<" is not deleted!"<<endl; return false; } } } else { if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished { bSearchFinished = TRUE; } else bSearchFinished = TRUE; //Terminate Search } } FindClose(hSearch); return true;}
//说明://[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;// 如: DeleteFile("F:/here/file.txt");等同于DeleteFile("F://here//file.txt");//[3] 该函数删除指定目录下的所有文件,并递归删除子文件夹的内容bool DeleteFileDir(string rootDir){ if (strcmp(rootDir.c_str(),"")==0 ||strcmp(rootDir.c_str(),"//")==0) { cout<<"非法路径!"<<endl; return false; } WIN32_FIND_DATA fd; ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); HANDLE hSearch; string filePathName; string tmpPath; filePathName=rootDir; BOOL bSearchFinished = FALSE; if( filePathName.data()[filePathName.length()-1] != '//' ) { filePathName+="//"; } filePathName+="*"; hSearch = FindFirstFile(filePathName.c_str(), &fd); if (hSearch==INVALID_HANDLE_VALUE) { cout<<"Directory or file name is invalid!"<<endl; return false; } while( !bSearchFinished ) { if( FindNextFile(hSearch, &fd) ) { if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; tmpPath+=fd.cFileName; //cout<<tmpPath.c_str()<< " is a directory! Not deleted."<<endl; if(!DeleteFileDir(tmpPath)) { cout<<"Delete "<<tmpPath.c_str()<<" failed!"<<endl; return false; } if(rmdir(tmpPath.c_str())==0) { cout<<"Directory "<<tmpPath.c_str()<<" is deleted!"<<endl; } } else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; if (tmpPath.data()[tmpPath.length()-1]!='//') { tmpPath+="//"; } tmpPath+=fd.cFileName; if (hSearch==INVALID_HANDLE_VALUE) { cout<<"Directory or file name is invalid!"<<endl; return false; } if(DeleteFile(tmpPath.c_str())) { cout<<"File "<<tmpPath.c_str()<<" is deleted!"<<endl; } else { cout<<"File "<<tmpPath.c_str()<<" is not deleted!"<<endl; return false; } } } else { if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished { bSearchFinished = TRUE; } else bSearchFinished = TRUE; //Terminate Search } } FindClose(hSearch); return true;}
//说明://[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;// 如: CopyFile("F:/here/file.txt","I:");等同于CopyFile("F://here//file.txt","I://");//[3] 该函数拷贝指定目录下的所有文件到指定目标目录, 不递归拷贝子文件夹的内容//[4] fileType : *.* , *.txt , *.doc 等等形式 bool CopyFileInDir(string rootDir,string destDir,string fileType){ if (strcmp(rootDir.c_str(),"")==0 ||strcmp(rootDir.c_str(),"//")==0) { cout<<"非法源路径!"<<endl; return false; }
if (strcmp(destDir.c_str(),"")==0 ||strcmp(destDir.c_str(),"//")==0) { cout<<"非法目标路径!"<<endl; return false; } if (!IsFileDirExist(destDir)) { CreateDirectory(destDir.c_str(),NULL); } WIN32_FIND_DATA fd; ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); HANDLE hSearch; string filePathName; string tmpPath; string tmpDestPath; filePathName=rootDir; BOOL bSearchFinished = FALSE; if( filePathName.data()[filePathName.length()-1] != '//' ) { filePathName+="//"; } filePathName+= fileType; hSearch = FindFirstFile(filePathName.c_str(), &fd);
if (hSearch==INVALID_HANDLE_VALUE) { cout<<"无效文件名"<<endl; return false; } while( !bSearchFinished ) { if( FindNextFile(hSearch, &fd) ) { if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; if( tmpPath.data()[tmpPath.length() -1] != '//' ) { tmpPath+="//"; } tmpPath+= fd.cFileName; cout<<tmpPath.c_str()<<" is a directory!"<<endl; } else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; if( tmpPath.data()[tmpPath.length() -1] != '//' ) { tmpPath+="//"; } tmpPath+= fd.cFileName; tmpDestPath = destDir; if( tmpDestPath.data()[tmpDestPath.length() -1] != '//' ) { tmpDestPath+= "//"; } tmpDestPath+=fd.cFileName; if(CopyFile(tmpPath.c_str(),tmpDestPath.c_str(),FALSE)) cout<<"Copy file From "<<tmpPath.c_str()<<" To " <<tmpDestPath.c_str()<<" successfully!"<<endl; else return false; } } else { if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished { bSearchFinished = TRUE; } else bSearchFinished = TRUE; //Terminate Search } } FindClose(hSearch); return true;}
//说明://[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;// 如: CopyFile("F:/here/file.txt","I:");等同于CopyFile("F://here//file.txt","I://");//[3] 该函数拷贝指定目录下的所有文件,并递归拷贝子文件夹的内容到指定目标目录//[4] fileType : *.* , *.txt , *.doc 等等形式 bool CopyFileDir(string rootDir,string destDir,string fileType){ if (strcmp(rootDir.c_str(),"")==0 ||strcmp(rootDir.c_str(),"//")==0) { cout<<"非法源路径!"<<endl; return false; }
if (strcmp(destDir.c_str(),"")==0 ||strcmp(destDir.c_str(),"//")==0) { cout<<"非法目标路径!"<<endl; return false; } if (!IsFileDirExist(destDir)) { if(CreateDirectory(destDir.c_str(),NULL)) { cout<<"Create Directory "<<destDir.c_str()<<endl; } } WIN32_FIND_DATA fd; ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); HANDLE hSearch; string filePathName; string tmpPath; string tmpDestPath; filePathName=rootDir; BOOL bSearchFinished = FALSE; if( filePathName.data()[filePathName.length()-1] != '//' ) { filePathName+="//"; } filePathName+= fileType; hSearch = FindFirstFile(filePathName.c_str(), &fd);
if (hSearch==INVALID_HANDLE_VALUE) { cout<<"无效文件名"<<endl; return false; } while( !bSearchFinished ) { if( FindNextFile(hSearch, &fd) ) { if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; if( tmpPath.data()[tmpPath.length() -1] != '//' ) { tmpPath+="//"; } tmpPath+= fd.cFileName; tmpDestPath = destDir; if( tmpDestPath.data()[tmpDestPath.length() -1] != '//' ) { tmpDestPath+= "//"; } tmpDestPath+=fd.cFileName; if(!CopyFileDir(tmpPath,tmpDestPath,fileType)) { return false; } } else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") ) { tmpPath = rootDir; if( tmpPath.data()[tmpPath.length() -1] != '//' ) { tmpPath+="//"; } tmpPath+= fd.cFileName; tmpDestPath = destDir; if( tmpDestPath.data()[tmpDestPath.length() -1] != '//' ) { tmpDestPath+= "//"; } tmpDestPath+=fd.cFileName; if(CopyFile(tmpPath.c_str(),tmpDestPath.c_str(),FALSE)) cout<<"Copy file From "<<tmpPath.c_str()<<" To " <<tmpDestPath.c_str()<<" successfully!"<<endl; else return false; } } else { if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished { bSearchFinished = TRUE; } else bSearchFinished = TRUE; //Terminate Search } } FindClose(hSearch); return true;}