String PathUtil::GetRelativePath(const String &path, const String &rootPath) { String lowerFileOrPath = path; String lowerRootPath = rootPath; FormatPath(lowerFileOrPath, true); FormatPath(lowerRootPath, true); if(!IsEndWithSeperator(lowerRootPath)) { lowerRootPath += SEPERATOR; } size_t ret = lowerFileOrPath.find(lowerRootPath); if(ret == 0) { String result = path.substr(lowerRootPath.length()); return result; } else { return INVALID_PATH; } return INVALID_PATH; }
bool PathUtil::CopyDir(const String &srcDir, const String &destDir, bool bOverWrite) { DIR *pDir; struct dirent *pDirInfo; struct stat st; String tempSrcDir = srcDir; String tempDestDir = destDir; FormatPath(tempSrcDir); FormatPath(tempDestDir); std::string strDir = StringUtil::ConvMBS(tempSrcDir); const char *dirname = strDir.c_str(); // open dirent directory if((pDir = opendir(dirname)) == NULL) return false; // read all files in this dir while((pDirInfo = readdir(pDir)) != NULL) { char fullname[255]; memset(fullname, 0, sizeof(fullname)); /* ignore hidden files */ if(pDirInfo->d_name[0] == '.') continue; strncpy(fullname, dirname, sizeof(fullname)); strncat(fullname, pDirInfo->d_name, sizeof(fullname)); /* get dirent status */ if(stat(fullname, &st) == -1) return false; /* if dirent is a directory, call itself */ if(S_ISDIR(st.st_mode)) { String subDir = StringUtil::ConvStr(fullname) + SEPERATOR; String subDestDir = tempDestDir + GetPureDirname(subDir); if(!IsEndWithSeperator(subDestDir)) subDestDir += SEPERATOR; PathUtil::CopyDir(subDir, subDestDir, bOverWrite); } else { String tempEntry = StringUtil::ConvStr(pDirInfo->d_name); String subSrcDir = StringUtil::ConvStr(dirname); String destFile = tempDestDir + tempEntry; PathUtil::CopyFilePath(subSrcDir, destFile, bOverWrite); } } // while return true; }
void CDuyetFile::WriteElementToFileText(const INFOFILE& infoFile, const HANDLE &hFile) { DWORD dwPtr = SetFilePointer( hFile, 0, NULL, FILE_END); TCHAR strTemp[MAX_PATH] = {0}; const TCHAR* strPath = FormatPath(infoFile.strFileName); //Write file name. StringCchPrintf(strTemp, MAX_PATH, _T("%s%s"),strPath, _T("\r\n") ); WriteFile(hFile, strTemp, wcslen(strTemp) * sizeof(TCHAR), &dwPtr, NULL); //Write type file. wmemset(strTemp, 0, MAX_PATH); StringCchPrintf(strTemp, MAX_PATH, _T("\r\t%d\r\n"), infoFile.cTypeFile); WriteFile(hFile, strTemp, wcslen(strTemp) * sizeof(TCHAR), &dwPtr, NULL); //Write file size. wmemset(strTemp, 0, MAX_PATH); StringCchPrintf(strTemp, MAX_PATH, _T("\r\t%I64u\r\n"), infoFile.llSize); WriteFile(hFile, strTemp, wcslen(strTemp) * sizeof(TCHAR), &dwPtr, NULL); //Write last modified. wmemset(strTemp, 0, MAX_PATH); StringCchPrintf(strTemp, MAX_PATH, _T("\r\t%I64u\r\n"), infoFile.llLastModified); WriteFile(hFile, strTemp, wcslen(strTemp) * sizeof(TCHAR), &dwPtr, NULL); }
bool CMountProg::ReadPathsFromFile(char* sFileName) { sFileName = FormatPath(sFileName, FORMAT_PATH); std::ifstream pathFile(sFileName); if (pathFile.is_open()) { std::string line; while (std::getline(pathFile, line)) { char *pCurPath = (char*)malloc(line.size() + 1); pCurPath = (char*)line.c_str(); if (pCurPath != NULL) { char curPathAlias[MAXPATHLEN]; strcpy_s(curPathAlias, pCurPath); char *pCurPathAlias = (char*)malloc(strlen(curPathAlias)); pCurPathAlias = curPathAlias; Export(pCurPath, pCurPathAlias); } } } else { printf("Can't open file %s.\n", sFileName); return false; } return true; }
WIN32_FIND_DATA * CFileManager::GetFirstNextFile( const char * fileFilter, bool first ){ bool Success; if( first ){ if( FirstNextHandle ){ FindClose( FirstNextHandle ); FirstNextHandle=NULL; } char Path[MAX_PATH]; strcpy( Path, fileFilter ); if( !FormatPath( Path ) ) return 0; FirstNextHandle = FindFirstFile( Path, &LAST_FIRSTNEXT_DATA ); Success = FirstNextHandle != INVALID_HANDLE_VALUE; } else{ if( FirstNextHandle==NULL ) return NULL; Success = FindNextFile( FirstNextHandle, &LAST_FIRSTNEXT_DATA )>0; } if( !Success ){ FindClose( FirstNextHandle ); FirstNextHandle=NULL; } return &LAST_FIRSTNEXT_DATA; }
// FormatAppCommandLine // Formats a command line used to call the application contained in // m_sApplication. // Assumptions: // The application name is either complete or ends in a reconginized // executable extension. The application name will not cause // confusion when it is executed (there isn't an .exe and a .bat // file in the same path). The operating system is not case // sensitive. The case application name has already been lowered. // Side affects: // Places the formatted command line in m_sAppCommandLine. void CCustinfoDriverApp::FormatAppCommandLine() { FormatPath(); m_sAppCommandLine = m_CmdInfo.m_sPath + m_CmdInfo.m_sApplication; FormatStringParameter(m_sAppCommandLine); if (m_CmdInfo.m_sApplication != m_sAppNames[reports]) { m_sAppCommandLine += ' '; if (m_CmdInfo.m_sApplication == m_sAppNames[custinfo]) { m_sAppCommandLine = m_sAppCommandLine + m_cFlagChar + CArborCommandLineInfo::GetMinimizeFlag(); } if (!m_CmdInfo.m_sUsername.IsEmpty()) { m_sAppCommandLine = m_sAppCommandLine + ' ' + m_cFlagChar + CArborCommandLineInfo::GetUserFlag() + ' ' + m_CmdInfo.m_sUsername; } if (!m_CmdInfo.m_sPassword.IsEmpty()) { m_sAppCommandLine = m_sAppCommandLine + ' ' + m_cFlagChar + CArborCommandLineInfo::GetPasswordFlag() + ' ' + m_CmdInfo.m_sPassword; } if (!m_CmdInfo.m_sServer.IsEmpty()) { m_sAppCommandLine = m_sAppCommandLine + ' ' + m_cFlagChar + CArborCommandLineInfo::GetServerFlag() + ' ' + m_CmdInfo.m_sServer; } if (!m_CmdInfo.m_sDatabase.IsEmpty()) { m_sAppCommandLine = m_sAppCommandLine + ' ' + m_cFlagChar + CArborCommandLineInfo::GetDatabaseFlag() + ' ' + m_CmdInfo.m_sDatabase; } if (!m_CmdInfo.m_sLogfile.IsEmpty()) { FormatStringParameter(m_CmdInfo.m_sLogfile); m_sAppCommandLine = m_sAppCommandLine + ' ' + m_cFlagChar + CArborCommandLineInfo::GetLogfileFlag() + ' ' + m_CmdInfo.m_sLogfile; } if (!m_CmdInfo.m_sLanguage.IsEmpty()) { m_sAppCommandLine = m_sAppCommandLine + ' ' + m_cFlagChar + CArborCommandLineInfo::GetLanguageFlag() + ' ' + m_CmdInfo.m_sLanguage; } } }
String PathUtil::GetHttpSafeFilePath(const String &httpfile) { String ret = httpfile; FormatPath(ret); StringUtil::Trim(ret); ret = StringUtil::Replace(ret, _T(" "), _T("%20")); return ret; }
bool PathUtil::DelFilesInDir(const String &dir) { DIR *pDir; struct dirent *pDirInfo; struct stat st; String tempDir = dir; FormatPath(tempDir); if(IsEndWithSeperator(tempDir)) tempDir = tempDir.substr(0, tempDir.length() -1); std::string strDir = StringUtil::ConvMBS(tempDir); const char *dirname = strDir.c_str(); // open dirent directory if((pDir = opendir(dirname)) == NULL) return false; // read all files in this dir while((pDirInfo = readdir(pDir)) != NULL) { char fullname[255]; memset(fullname, 0, sizeof(fullname)); /* ignore hidden files */ if(pDirInfo->d_name[0] == '.') continue; strncpy(fullname, dirname, sizeof(fullname)); strncat(fullname, "/", sizeof(fullname)); strncat(fullname, pDirInfo->d_name, sizeof(fullname)); /* get dirent status */ if(stat(fullname, &st) == -1) return false; /* if dirent is a directory, call itself */ if(S_ISDIR(st.st_mode)) { String subDir = StringUtil::ConvStr(fullname); if(DelFilesInDir(subDir)) { if(rmdir(fullname) != 0) return false; } else return false; } else { // delete file if(remove(fullname) != 0) return false; } } return true; }
String PathUtil::GetDrive(const String &path) { String tempPath = path; FormatPath(tempPath); size_t pos = tempPath.find(_T(":/")); if(!pos) return INVALID_PATH; return tempPath.substr(0, pos + 2); }
bool PathUtil::IsDriveOrRoot(const String &path) { String tempPath = path; FormatPath(tempPath); if( path[0] == SEPERATOR || // unix/linux mount point ((path.length() == 2 || path.length() ==3) && path[1] == _T(':')) // windows drive, like c:/ ) return true; return false; }
String PathUtil::GetHttpFileInPath(const String &httpfile) { String ret = httpfile; FormatPath(ret); size_t pos = ret.find_last_of(SEPERATOR); if(pos == String::npos) return INVALID_PATH; return ret.substr(0, pos + 1); }
String PathUtil::GetParentPath(const String &fileOrPath) { String tempFile = fileOrPath; FormatPath(tempFile); if(IsDir(tempFile)) { tempFile = tempFile.substr(0, tempFile.length() - 1); } tempFile = GetFileDirPath(tempFile); return tempFile; }
String PathUtil::GetRenameExtFile(const String &filename, const String &extWithDot) { String newFilename = filename; newFilename = StringUtil::Replace(newFilename, BACKSLASH, SLASH); size_t dotPos = newFilename.find_last_of(DOT); newFilename = newFilename.substr(0, dotPos) + extWithDot; FormatPath(newFilename); return newFilename; }
void PathUtil::EnumSubDirs( StringArray &ret, const String &rootPath, bool bIncSubDirs) { DIR *pDir; struct dirent *pDirInfo; struct stat st; String tempRootDir = rootPath; FormatPath(tempRootDir); if(IsEndWithSeperator(tempRootDir)) tempRootDir = tempRootDir.substr(0, tempRootDir.length() -1); std::string strDir = StringUtil::ConvMBS(tempRootDir); const char *szDir = strDir.c_str(); // open dirent directory if((pDir = opendir(szDir)) == NULL) return ; // read all files in this dir while((pDirInfo = readdir(pDir)) != NULL) { char fullname[255]; memset(fullname, 0, sizeof(fullname)); /* ignore hidden files */ if(pDirInfo->d_name[0] == '.') continue; strncpy(fullname, szDir, sizeof(fullname)); strncat(fullname, "/", sizeof(fullname)); strncat(fullname, pDirInfo->d_name, sizeof(fullname)); /* get dirent status */ if(stat(fullname, &st) == -1) return ; /* if dirent is a directory, call itself */ if(S_ISDIR(st.st_mode)) { if(bIncSubDirs) { String subFolder = StringUtil::ConvStr(fullname); ret.push_back(subFolder); EnumSubDirs(ret, subFolder, bIncSubDirs); } } } // while }
String PathUtil::GetFileDirPath(const String &filename) { String path = filename; FormatPath(path); size_t pos = path.find_last_of(SEPERATOR); if(pos == String::npos) { return INVALID_PATH; } path = path.substr(0, pos + 1); return path; }
void CMountProg::Export(char *path, char *pathAlias) { path = FormatPath(path, FORMAT_PATH); if (path != NULL) { pathAlias = FormatPathAlias(pathAlias); if (m_PathMap.count(pathAlias) == 0) { m_PathMap[pathAlias] = path; printf("Path #%i is: %s, path alias is: %s\n", m_PathMap.size(), path, pathAlias); } else { printf("Path %s with path alias %s already known\n", path, pathAlias); } } }
bool CSADirRead::GetDirs(const char * pDirPath, bool bRecurse) { m_sSourceDir = pDirPath; FormatPath(m_sSourceDir); // find tree starting from pDirPath if (bRecurse) { GetSubDirs(m_dirs, m_sSourceDir); } // add the main directory. m_dirs.push_back(CSADirEntry(m_sSourceDir)); return true; }
String PathUtil::GetDriveOrRoot(const String &path) { String tempPath = path; FormatPath(tempPath); size_t pos = tempPath.find(_T(":/")); if(pos) { tempPath = tempPath.substr(0, pos + 2); } else { if(tempPath[0] == SEPERATOR) tempPath = SEPERATOR; else tempPath = INVALID_PATH; } return tempPath; }
// chop off trailing "\" void CSADirRead::FormatPath(CCOMString &path) { CCOMString inPath = path; inPath.TrimRight(); CCOMString tmp; int iLastSlashPos = inPath.ReverseFind('\\'); if (iLastSlashPos == -1) { iLastSlashPos = inPath.ReverseFind('/'); } if (iLastSlashPos!=-1) { if (iLastSlashPos==inPath.GetLength()-1) { path = inPath.Left(iLastSlashPos); FormatPath(path); // in case the incoming path is "C:\temp\\\..." } } }
bool PathUtil::DelPath(const String &path) { struct stat st; String tempPath = path; FormatPath(tempPath); if(IsEndWithSeperator(tempPath)) tempPath = tempPath.substr(0, tempPath.length() -1); std::string strPath = StringUtil::ConvMBS(tempPath); const char *szPath = strPath.c_str(); /* get dirent status */ if(stat(szPath, &st) == -1) return false; bool flag = false; /* if dirent is a directory, call itself */ if(S_ISDIR(st.st_mode)) { if(DelFilesInDir(tempPath)) { if(rmdir(szPath) == 0) flag = true; } } else { // delete file if(remove(szPath) == 0) flag = true; } return flag; }
UINT CSADirRead::FindFiles(const CCOMString & dir, const CCOMString & filter, bool bIncludeFilesInFileList, bool bIncludeFoldersInFileList) { // make sure the path ends in a single "\" CCOMString baseName = dir; FormatPath(baseName); baseName+='\\'; CCOMString fullPath = baseName; fullPath += filter; CCOMString fileName; // find first file in current directory #ifndef USE_WIN32_FINDFILE struct _finddata_t c_file; long fhandle; try { if ((fhandle=_findfirst( fullPath, &c_file ))!=-1) { bool bIsFolder = (c_file.attrib & _A_SUBDIR)==_A_SUBDIR; bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || (!bIsFolder && bIncludeFilesInFileList); // skip . and .. if (bIsFolder && (strcmp(c_file.name, ".")==0) || (strcmp(c_file.name, "..")==0)) { bAddThisOne = false; } if (bAddThisOne) { fileName = baseName; fileName += c_file.name; CSAFileEntry t; t.bIsFolder = bIsFolder; t.attrib = c_file.attrib; t.m_sName = fileName; t.time_write = c_file.time_write; t.time_create = c_file.time_create; t.size = c_file.size; m_files.push_back(t); } // find the rest of them while(_findnext( fhandle, &c_file ) == 0 ) { bool bIsFolder = (c_file.attrib & _A_SUBDIR)==_A_SUBDIR; bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || (!bIsFolder && bIncludeFilesInFileList); // skip . and .. if (bIsFolder && (strcmp(c_file.name, ".")==0) || (strcmp(c_file.name, "..")==0)) { bAddThisOne = false; } if (bAddThisOne) { fileName=baseName; fileName += c_file.name; CSAFileEntry t; t.bIsFolder = bIsFolder; t.attrib = c_file.attrib; t.m_sName = fileName; t.time_write = c_file.time_write; t.time_create = c_file.time_create; t.size = c_file.size; m_files.push_back(t); } } _findclose(fhandle); } } catch (...) { return false; } #else WIN32_FIND_DATA FindFileData; HANDLE hFind; try { if ((hFind = FindFirstFile(fullPath, &FindFileData))!=INVALID_HANDLE_VALUE) { bool bIsFolder = (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY; bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || ((!bIsFolder) && bIncludeFilesInFileList); // skip . and .. if (bIsFolder && (strcmp(FindFileData.cFileName, ".")==0) || (strcmp(FindFileData.cFileName, "..")==0)) { bAddThisOne = false; } if (bAddThisOne) { fileName = baseName; fileName += FindFileData.cFileName; CSAFileEntry t; t.m_sName = fileName; t.bIsFolder = bIsFolder; t.attrib = FindFileData.dwFileAttributes; memcpy(&t.time_write, &FindFileData.ftLastWriteTime, sizeof(_FILETIME)); memcpy(&t.time_create, &FindFileData.ftCreationTime, sizeof(_FILETIME)); t.size = ((unsigned __int64)FindFileData.nFileSizeHigh * ((unsigned __int64)MAXDWORD+1)) + (unsigned __int64)FindFileData.nFileSizeLow; m_files.push_back(t); } // find the rest of them while (FindNextFile(hFind, &FindFileData)) { bool bIsFolder = (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY; bool bAddThisOne = (bIsFolder && bIncludeFoldersInFileList) || ((!bIsFolder) && bIncludeFilesInFileList); // skip . and .. if (bIsFolder && (strcmp(FindFileData.cFileName, ".")==0) || (strcmp(FindFileData.cFileName, "..")==0)) { bAddThisOne = false; } if (bAddThisOne) { fileName = baseName; fileName += FindFileData.cFileName; CSAFileEntry t; t.m_sName = fileName; t.bIsFolder = bIsFolder; t.attrib = FindFileData.dwFileAttributes; memcpy(&t.time_write, &FindFileData.ftLastWriteTime, sizeof(_FILETIME)); memcpy(&t.time_create, &FindFileData.ftCreationTime, sizeof(_FILETIME)); t.size = ((unsigned __int64)FindFileData.nFileSizeHigh * ((unsigned __int64)MAXDWORD+1)) + (unsigned __int64)FindFileData.nFileSizeLow; m_files.push_back(t); } } FindClose(hFind); } } catch (...) { return false; } #endif return true; }
int CFileManager::RunForeach( FileOPFunc function, const char * fileFilter, int nRecursive ){ char AbsPath[MAX_PATH]={0}; char AbsFilter[MAX_PATH]={0}; char FullPath[MAX_PATH]={0}; bool AddFilter = fileFilter[0]=='\0'; strcpy( FullPath, fileFilter ); if( !FormatPath( FullPath ) ) return 0; unsigned int nFind = 0; unsigned int nLen = strlen( FullPath ); unsigned int nAbsLen; for( unsigned int n=0;n<nLen;n++ ){ if( FullPath[n]=='\\'||FullPath[n]=='/' ) nFind = n; } if( nFind>0 ){ strncpy( AbsPath, FullPath, nFind+1 ); strcpy( AbsFilter, &FullPath[nFind+1] ); } else{ strcpy( AbsFilter, FullPath ); } nAbsLen = strlen( AbsPath ); HANDLE hFile; if( AbsFilter[0]=='\0' || AddFilter ){ strcpy( AbsFilter, "*.*" ); sprintf( FullPath, "%s%s", AbsPath, AbsFilter ); hFile = FindFirstFile( FullPath, &LAST_FILE_DATA ); } else hFile = FindFirstFile( FullPath, &LAST_FILE_DATA ); bool Success = hFile != INVALID_HANDLE_VALUE; char * actFileName; while( Success ){ if( !( LAST_FILE_DATA.cFileName[0]=='.' && LAST_FILE_DATA.cFileName[1]=='\0' ) && !( LAST_FILE_DATA.cFileName[0]=='.' && LAST_FILE_DATA.cFileName[1]=='.' && LAST_FILE_DATA.cFileName[2]=='\0' ) ){ FILES_COUNTED++; if( (nAbsLen+strlen( LAST_FILE_DATA.cFileName )) >= MAX_PATH ){ actFileName = &LAST_FILE_DATA.cAlternateFileName[0]; if( (nAbsLen+strlen( actFileName )) >= MAX_PATH ) actFileName = NULL; } else actFileName = &LAST_FILE_DATA.cFileName[0]; if( actFileName ){ sprintf( FullPath, "%s%s", AbsPath, actFileName ); if( function( this, FullPath ) > 0 ){ FindClose( hFile ); return 1; } if( nRecursive && (LAST_FILE_DATA.dwFileAttributes & (1 << 4 )) > 0 ){ if( (strlen( AbsFilter )+strlen( actFileName )+1) < MAX_PATH ){ sprintf( FullPath, "%s%s/%s", AbsPath, actFileName, AbsFilter ); if( RunForeach( function, FullPath, 1 ) > 0 ){ FindClose( hFile ); return 1; } } } } } Success = FindNextFile( hFile, &LAST_FILE_DATA )>0; } FindClose( hFile ); return 0; }
int CFileManager::EnsureFolderExistance( const char * folderpath, int CreateIfMissing ){ char RealFolder[MAX_PATH]={0}; strcpy( RealFolder, folderpath ); if( !FormatPath( RealFolder ) ) return 0; DWORD Attrib = GetFileAttributes( RealFolder ); if( GetLastError( ) == ERROR_BAD_NETPATH ) return 0; if( Attrib == INVALID_FILE_ATTRIBUTES ){ if( CreateIfMissing ){ if( CreateDirectory( RealFolder, NULL ) ) return 1; if( GetLastError( ) == ERROR_PATH_NOT_FOUND ){ char DriveLetter = RealFolder[0]; int nLen = strlen( RealFolder ); char * Path = (char*)calloc( nLen, 1 ); memcpy( Path, &RealFolder[3], nLen-3 ); char EffectivePath[MAX_PATH]={0}; if( !isalpha( DriveLetter ) ) return 3; for( int n=0;n<nLen;n++ ){ if( Path[n]=='\\' || Path[n]== '/' ){ Path[n]='\0'; sprintf( EffectivePath, "%c:\\%s", DriveLetter, Path ); CreateDirectory( EffectivePath, NULL ); Path[n]='\\'; } } free( Path ); if( CreateDirectory( RealFolder, NULL ) ) return 1; if( GetLastError( ) == ERROR_PATH_NOT_FOUND ) return 3; } } return 0; } else if( (Attrib & (1 << 4 )) > 0 ){ return 1; } return 2; }
int CFileManager::DestroyFolder( FileOPFunc function, const char * folderpath, int OnlyDestroyIfEmpty, int * failed ){ int nFailed = 0; int nReturn = 0; char AbsPath[260]; sprintf( AbsPath, "%s/*.*", folderpath ); if( !FormatPath( AbsPath ) ) return 0; DWORD Attrib = GetFileAttributes( folderpath ); if( (Attrib & (1 << 4 )) <= 0 || Attrib == INVALID_FILE_ATTRIBUTES ){ return 0; } WIN32_FIND_DATA filedata; HANDLE FileH = FindFirstFile( AbsPath, &filedata ); bool Success = FileH != INVALID_HANDLE_VALUE; unsigned int nBaseLen = strlen( folderpath ); int nFuncRet = 0; while( Success ){ if( !( filedata.cFileName[0]=='.' && filedata.cFileName[1]=='\0' ) && !( filedata.cFileName[0]=='.' && filedata.cFileName[1]=='.' && filedata.cFileName[2]=='\0' ) ){ if( OnlyDestroyIfEmpty ){ FindClose( FileH ); return 0; } if( nBaseLen+strlen( filedata.cFileName ) >= MAX_PATH ) sprintf( AbsPath, "%s/%s", folderpath, filedata.cAlternateFileName ); else sprintf( AbsPath, "%s/%s", folderpath, filedata.cFileName ); if( (filedata.dwFileAttributes & (1 << 4 )) > 0 ){ Success = FindNextFile( FileH, &filedata ) > 0; nReturn+=DestroyFolder( function, AbsPath, OnlyDestroyIfEmpty, failed ); } else{ if( function ) nFuncRet = function( this, AbsPath ); Success = FindNextFile( FileH, &filedata ) > 0; if( nFuncRet >= 2 ){ FindClose( FileH ); return nReturn; } else if( nFuncRet <= 0 ){ if( DeleteFile( AbsPath ) ) nReturn++; else nFailed++; } } } else Success = FindNextFile( FileH, &filedata ) > 0; } FindClose( FileH ); if( isalpha(folderpath[0]) && folderpath[1] == ':' && ( folderpath[2]=='/' || folderpath[2]=='\\' ) && folderpath[3] == '\0' ) nReturn++; else if( RemoveDirectory( folderpath ) ) nReturn++; else nFailed++; if( failed ) *failed += nFailed; return nReturn; }
int CFileManager::CopyFiles( FileOPFunc function, const char * fileFilter, const char * destination, int failIfExists, int nRecursive, int * failed ){ char Source[MAX_PATH]; char Dest[MAX_PATH]; char SubSource[MAX_PATH]; char * subFilter; strcpy( Source, fileFilter ); strcpy( Dest, destination ); if( !FormatPath( Source ) || !FormatPath( Dest ) ) return 0; EnsureFolderExistance( Dest, true ); DWORD Attrib = GetFileAttributes( Dest ); DWORD DestLen = strlen( Dest ); DWORD SourceLen = strlen( Source ); DWORD SubLen; int nFuncRet = 0; int nReturn = 0; HANDLE hFile = FindFirstFile( Source, &LAST_FILE_DATA ); bool Success = hFile != INVALID_HANDLE_VALUE; if( !Success ) return 0; for( unsigned int n=0;n<SourceLen;n++ ){ if( Source[n]=='\\' ){ nFuncRet=n; } } Source[nFuncRet]='\0'; subFilter = &Source[nFuncRet+1]; SourceLen=(nFuncRet-1); for( unsigned int n=0;n<DestLen;n++ ){ if( Dest[n]=='\\' ){ nFuncRet=n; } } if( (DestLen-1)==nFuncRet ){ Dest[nFuncRet]='\0'; DestLen--; } if( (Attrib & (1 << 4 )) <= 0 || Attrib == INVALID_FILE_ATTRIBUTES ){ return 0; } while( Success ){ if( !( LAST_FILE_DATA.cFileName[0]=='.' && LAST_FILE_DATA.cFileName[1]=='\0' ) && !( LAST_FILE_DATA.cFileName[0]=='.' && LAST_FILE_DATA.cFileName[1]=='.' && LAST_FILE_DATA.cFileName[2]=='\0' ) ){ SubLen=strlen( LAST_FILE_DATA.cFileName ); if( DestLen+SubLen+1 >= MAX_PATH ) sprintf( SubDestination, "%s\\%s", Dest, LAST_FILE_DATA.cAlternateFileName ); else sprintf( SubDestination, "%s\\%s", Dest, LAST_FILE_DATA.cFileName ); if( DestLen+SubLen+1 >= MAX_PATH ) sprintf( SubSource, "%s\\%s", Source, LAST_FILE_DATA.cAlternateFileName ); else sprintf( SubSource, "%s\\%s", Source, LAST_FILE_DATA.cFileName ); if( function ) nFuncRet = function( this, SubSource ); if( nFuncRet <= 1 ){ if( ((LAST_FILE_DATA.dwFileAttributes & (1 << 4 )) > 0) && nRecursive>0 ){ strcat( SubSource, "\\" ); strcat( SubSource, subFilter ); nReturn+=CopyFiles( function, SubSource, SubDestination, failIfExists, nRecursive, failed ); } else if( nFuncRet<= 0 ){ if( CopyFile( SubSource, SubDestination, failIfExists ) ){ nReturn++; } else if( failed != 0 ){ *failed+=1; } } } else if( nFuncRet >= 2 ){ FindClose( hFile ); return nReturn; } } Success = FindNextFile( hFile, &LAST_FILE_DATA )>0; } FindClose( hFile ); return nReturn+1; }