static BOOL FindNextChildDir (HANDLE hFindFile, WIN32_FIND_DATA *lpFindData) { BOOL fFound = FALSE; do { fFound = FindNextFile(hFindFile, lpFindData); } while (fFound && !IsChildDir(lpFindData)); return(fFound); }
bool getDirs(string pathString, DirsFiles& vec){ WIN32_FIND_DATA FindFileData; HANDLE hFind; wstring wStr_tmp; string str_tmp; wstring wstr(pathString.length(),L''); copy(pathString.begin(),pathString.end(),wstr.begin()); const wchar_t* path = wstr.c_str(); if(!SetCurrentDirectory(path)){ printf("%s Not a directory.\n",path); return false; } hFind = FindFirstFile(L"*", &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { cerr << "Invalid file handle. Error code: " << GetLastError() << endl; return false; } else { //wcout << L"File found: " << FindFileData.cFileName << endl; while(FindNextFile(hFind, &FindFileData)){ //wcout << L"File found: " << FindFileData.cFileName << endl; if(IsChildDir(&FindFileData)){ wStr_tmp = FindFileData.cFileName; str_tmp = string(wStr_tmp.begin(), wStr_tmp.end()); str_tmp.assign(wStr_tmp.begin(), wStr_tmp.end()); vec.push_back(str_tmp); //vec.push_back(FindFileData.cFileName); } } FindClose(hFind); if (GetLastError() == ERROR_NO_MORE_FILES){ //cout << "\nSearch finished." << endl; } else{ cout << "\nUnknown error." << endl; return false; } } return true; }
static HANDLE FindFirstChildDir (LPTSTR szPath, WIN32_FIND_DATA *lpFindData) { BOOL fFound; HANDLE hFindFile = FindFirstFile(szPath, lpFindData); if (hFindFile != INVALID_HANDLE_VALUE) { fFound = IsChildDir(lpFindData); if (!fFound) fFound = FindNextChildDir(hFindFile, lpFindData); if (!fFound) { FindClose(hFindFile); hFindFile = INVALID_HANDLE_VALUE; } } return(hFindFile); }
// Walk the directory structure and fill a list box with // filenames. If pDW->fRecurse is set, list any child // directories by recursively calling DirWalkRecurse. static void DirWalkRecurse (LPDIRWALKDATA pDW) { HANDLE hFind; pDW->nDepth++; pDW->nIndent = 3 * pDW->nDepth; _stprintf(pDW->szBuf, __TEXT("%*s"), pDW->nIndent, __TEXT("")); GetCurrentDirectory(chDIMOF(pDW->szBuf) - pDW->nIndent, &pDW->szBuf[pDW->nIndent]); //store data in filenamelist /* _FileNameData fnd; memcpy(fnd.buf, pDW->szBuf, BUF_SIZE); fileNameList.push_back(fnd); */ //end hFind = FindFirstFile(__TEXT("*.*"), &pDW->FindData); pDW->fOk = (hFind != INVALID_HANDLE_VALUE); while (pDW->fOk) { pDW->fIsDir = pDW->FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; if (!pDW->fIsDir || (!pDW->fRecurse && IsChildDir(&pDW->FindData))) { _stprintf(pDW->szBuf, pDW->fIsDir ? __TEXT("%*s[%s]") : __TEXT("%*s%s"), pDW->nIndent, __TEXT(""), pDW->FindData.cFileName); //ListBox_AddString(pDW->hwndTreeLB, pDW->szBuf); _FileNameData fnd; memcpy(fnd.buf, pDW->szBuf, BUF_SIZE); fileNameList.push_back(fnd); } pDW->fOk = FindNextFile(hFind, &pDW->FindData); } if (hFind != INVALID_HANDLE_VALUE) FindClose(hFind); if (pDW->fRecurse) { // Get the first child directory. hFind = FindFirstChildDir( __TEXT("*.*"), &pDW->FindData); pDW->fOk = (hFind != INVALID_HANDLE_VALUE); while (pDW->fOk) { // Change into the child directory. if (SetCurrentDirectory(pDW->FindData.cFileName)) { // Perform the recursive walk into the child // directory. Remember that some members of pDW // will be overwritten by this call. DirWalkRecurse(pDW); // Change back to the child's parent directory. SetCurrentDirectory(__TEXT("..")); } pDW->fOk = FindNextChildDir(hFind, &pDW->FindData); } if (hFind != INVALID_HANDLE_VALUE) FindClose(hFind); } pDW->nDepth--; }
static void DirWalkRecurse(LPDIRWALKDATA pDW, LPCTSTR pszRootPath, LPCTSTR pszDesPath) //pointer to DIRWALKDATA as parameter, so the param in the struct can be constantly updated such as nDepth { HANDLE hFind; //FindFirstFile()返回的句柄 作为FindNextFile()的参数,多次查找的关联 LPTSTR src = (LPTSTR)malloc(100 * sizeof(LPTSTR)); LPTSTR des = (LPTSTR)malloc(100 * sizeof(LPTSTR)); pDW->nDepth++; //recursive depth + 1 pDW->nIndent = 3 * pDW->nDepth; //控制缓冲区写入位置 _stprintf_s(pDW->szBuf, _TEXT("%*s"), pDW->nIndent, _TEXT("")); GetCurrentDirectory(chDIMOF(pDW->szBuf) - pDW->nIndent, &pDW->szBuf[pDW->nIndent]); //将当前目录存入缓冲区 //printf("%s\n", pDW->szBuf); hFind = FindFirstFile(_TEXT("*.*"), &pDW->FindData); //system API pDW->fOk = (hFind != INVALID_HANDLE_VALUE); while (pDW->fOk) //当前目录搜索完毕则跳出 { pDW->fIsDir = pDW->FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; //是否是目录 if (!pDW->fIsDir || (!pDW->fRecurse && IsChildDir(&pDW->FindData))) //为文件非目录 || (非递归 && 为一个目录文件) { _stprintf_s(pDW->szBuf, pDW->fIsDir ? _TEXT("%*s[%s]") : _TEXT("%*s%s"), pDW->nIndent, _TEXT(""), pDW->FindData.cFileName); //封装路径 lstrcpy(src, pszRootPath); lstrcat(src, "\\"); lstrcat(src, pDW->FindData.cFileName); lstrcpy(des, pszDesPath); lstrcat(des, "\\"); lstrcat(des, pDW->FindData.cFileName); //创建/打开文件 HANDLE desFile = CreateFile( des, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, pDW->FindData.dwFileAttributes, NULL); HANDLE srcFile = CreateFile( src, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, pDW->FindData.dwFileAttributes, NULL); DWORD filesize = GetFileSize(srcFile, NULL); char* buffer = new char[filesize + 1]; DWORD readsize; ReadFile(srcFile, buffer, filesize, &readsize, NULL); WriteFile(desFile, buffer, filesize, &readsize, NULL); buffer[filesize] = 0; CloseHandle(srcFile); CloseHandle(desFile); //printf("%s\n", pDW->szBuf); } else { SECURITY_ATTRIBUTES attribute; attribute.nLength = sizeof(attribute); attribute.lpSecurityDescriptor = NULL; attribute.bInheritHandle = FALSE; SetCurrentDirectory(pszDesPath); CreateDirectory(pDW->FindData.cFileName, &attribute); SetCurrentDirectory(pszRootPath); } pDW->fOk = FindNextFile(hFind, &pDW->FindData); //system API } if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); } if (pDW->fRecurse) { //Get the first child directory hFind = FindFirstChildDir(_TEXT("*.*"), &pDW->FindData); //找到第一个子目录 FindFirstFile pDW->fOk = (hFind != INVALID_HANDLE_VALUE); while (pDW->fOk) { //change into the child directory if (SetCurrentDirectory(pDW->FindData.cFileName)) { //封装路径 lstrcpy(src, pszRootPath); lstrcat(src, "\\"); lstrcat(src, pDW->FindData.cFileName); lstrcpy(des, pszDesPath); lstrcat(des, "\\"); lstrcat(des, pDW->FindData.cFileName); DirWalkRecurse(pDW, src, des); //change back to the child's parent dir SetCurrentDirectory(_TEXT("..")); } pDW->fOk = FindNextChildDir(hFind, &pDW->FindData); //将hFind句柄传入 FindNextFile } if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); } } pDW->nDepth--; }