#include <windows.h> #include <iostream> #include"tchar.h" using namespace std; #define MAXSIZE 100 void TraveDir(TCHAR* psDirName); int main() { TCHAR psDirName[MAXSIZE] = TEXT("F:\SQLyaog\SQLyog Trial"); TraveDir((TCHAR *) psDirName); return 0; } void TraveDir(TCHAR *psDirName) { WIN32_FIND_DATA FileData; HANDLE hSearch; TCHAR szDir[MAXSIZE]; TCHAR szNewPath[MAXSIZE]; TCHAR szDirSpace[MAXSIZE]; TCHAR szFullPath[MAXSIZE]; BOOL bworking; //memset(szNewPath,0,MAXSIZE); hSearch = FindFirstFile((LPCTSTR )psDirName,&FileData);//判断psDirName文件目录是否存在。 if(hSearch == INVALID_HANDLE_VALUE) { return ;//不存在就终止执行 } lstrcpy(szDir,psDirName); lstrcat(szDir,TEXT("\*.*"));//判断psDirName文件目录是否存在文件或文件夹 hSearch = FindFirstFile(szDir,&FileData); if(hSearch == INVALID_HANDLE_VALUE) { cout<<"error"<<endl; return ; } bworking = TRUE; while(bworking == TRUE)//遍历psDirName文件目录找出该目录下的文件夹,遍历子文件夹这样才能完整找出满足需求的文件。 { bworking = FindNextFile(hSearch,&FileData); if(FileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && //去除当前目录下的:"."当前目录,".."上层目录(隐藏目录) strcmp((char *)FileData.cFileName,".") && strcmp((char *)FileData.cFileName,"..")) { lstrcpy(szNewPath,(LPCWSTR)psDirName); lstrcat(szNewPath,TEXT("\")); lstrcat(szNewPath,FileData.cFileName); LPCTSTR Message=_T("消息"); MessageBox(NULL,szNewPath,Message,MB_OK); TraveDir((TCHAR *)szNewPath);//递归遍历所用目录包括子目录 } } lstrcpy(szDirSpace,(LPCWSTR)psDirName); lstrcat(szDirSpace,TEXT("\*.exe")); hSearch = FindFirstFile(szDirSpace,&FileData); /*if(hSearch != INVALID_HANDLE_VALUE) { bworking = TRUE; while(bworking == TRUE) { bworking = FindNextFile(hSearch,&FileData); lstrcpy(szDirPath,FileData.cFileName); LPCTSTR Message=_T("消息"); MessageBox(NULL,szDirPath,Message,MB_OK); for(int StrCount=0;int(*(szDirPath+StrCount))!=0;StrCount++) { printf("%c",*(szDirPath+StrCount)); } cout<<endl; } }*/ //记得下面与上面作比较,在这里与CFileFind 类处理的方法有一点不同,在CFileFind 类中GetFileName()要 //在FindNextFileA()函数调用之后。在这里要输出完整的搜索,要首先调用cFileName得到第一个满足要求的文件名, //在调用FindNextFile函数,不能搞反了。 if(hSearch != INVALID_HANDLE_VALUE) { bworking = TRUE; while(bworking) { lstrcpy(szFullPath,(LPCWSTR)psDirName); lstrcat(szFullPath,(LPCWSTR)"\"); lstrcat(szFullPath,FileData.cFileName); LPCTSTR Message=_T("消息"); MessageBox(NULL,szFullPath,Message,MB_OK); bworking = FindNextFile(hSearch,&FileData);//遍历当前目录的所用满足要求的文件 for(int StrCount=0;int(*(szFullPath+StrCount))!=0;StrCount++) { printf("%c",*(szFullPath+StrCount)); } cout<<endl; lstrcat(szFullPath,(LPCWSTR)"