- //转自:http://www.vcgood.com/forum_posts.asp?TID=2261&PN=1
- 版本1:用string处理,方便,容易理解.
- #include <windows.h>
- #include <iostream>
- #include <string>
- using namespace std;
- bool IsRoot(string Path)
- {
- string Root;
- Root=Path.at(0)+":\\";
- if(Root==Path)
- return true;
- else
- return false;
- }
- void FindInAll(string Path)
- {
- string szFind;
- szFind=Path;
- if(!IsRoot(szFind))
- szFind+="\\";
- szFind+="*.*";
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind=FindFirstFile(szFind.c_str(),& FindFileData);
- if(hFind==INVALID_HANDLE_VALUE)
- return ;
- do
- {
- if(FindFileData.cFileName[0]=='.')
- continue;
- if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- string szFile;
- if(IsRoot(Path))
- szFile=Path+FindFileData.cFileName;
- else
- szFile=Path+"\\"+FindFileData.cFileName;
- FindInAll(szFile);
- }
- else
- {
- string szFile;
- if(IsRoot(Path))
- szFile=Path+FindFileData.cFileName;
- else
- szFile=Path+"\\"+FindFileData.cFileName;
- cout<<szFile<<endl;
- cout<<FindFileData.cFileName<<endl;
- }
- }
- while(FindNextFile(hFind,& FindFileData));
- FindClose(hFind);
- }
- int main()
- {
- FindInAll("D:\\C++");
- return 0;
- }
- 版本2:编译器的通用性更强
- #include <windows.h>
- #include <iostream>
- using namespace std;
- BOOL IsRoot(LPCTSTR lpszPath)
- {
- TCHAR szRoot[4];
- wsprintf(szRoot,"%c:\\",lpszPath[0]);
- return (lstrcmp(szRoot,lpszPath)==0);
- }
- void FindInAll(::LPCTSTR lpszPath)
- {
- TCHAR szFind[MAX_PATH];
- lstrcpy(szFind,lpszPath);
- if(!IsRoot(szFind))
- lstrcat(szFind,"\\");
- lstrcat(szFind,"*.*");
- WIN32_FIND_DATA wfd;
- HANDLE hFind=FindFirstFile(szFind,& wfd);
- if(hFind==INVALID_HANDLE_VALUE)
- return;
- do
- {
- if(wfd.cFileName[0]=='.')
- continue;
- if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- TCHAR szFile[MAX_PATH];
- if(IsRoot(lpszPath))
- wsprintf(szFile,"%s%s",lpszPath,wfd.cFileName);
- else
- wsprintf(szFile,"%s\\%s",lpszPath,wfd.cFileName);
- FindInAll(szFile);
- }
- else
- {
- TCHAR szFile[MAX_PATH];
- if(IsRoot(lpszPath))
- wsprintf(szFile,"%s%s",lpszPath,wfd.cFileName);
- else
- wsprintf(szFile,"%s\\%s",lpszPath,wfd.cFileName);
- printf("%s\n",szFile);
- }
- }
- while(FindNextFile(hFind,&wfd));
- FindClose(hFind);
- }
- int main()
- {
- FindInAll("D:\\C++");
- return 0;
- }
发表于 @ 2008年08月02日 22:14:00|评论(loading...)|收藏