硬件/系统 遍历整个目录 |
#include <windows.h> #include <shlobj.h> //浏览目录. void BrowseFolder( void ) { TCHAR path[MAX_PATH]; BROWSEINFO bi = { 0 }; bi.lpszTitle = ("递归调用所有目录"); LPITEMIDLIST pidl = SHBrowseForFolder ( &bi ); if ( pidl != 0 ) { //获取目录路径 SHGetPathFromIDList ( pidl, path ); //设置为当前路径 SetCurrentDirectory ( path ); //搜索所有子目录 SearchFolder( path ); //释放内存 IMalloc * imalloc = 0; if ( SUCCEEDED( SHGetMalloc ( &imalloc )) ) { imalloc->Free ( pidl ); imalloc->Release ( ); } } //搜索其下所有子目录及文件. void SearchFolder( TCHAR * path ) { WIN32_FIND_DATA FindFileData; HANDLE hFind; TCHAR filename[ MAX_PATH + 256 ]; TCHAR pathbak[ MAX_PATH ]; //复制初始用户选择目录 strcpy( pathbak, path ); //寻找第一个文件 hFind = FindFirstFile ( "*.*", &FindFileData ); //搜索所有文件及子目录 do { if ( hFind != INVALID_HANDLE_VALUE ) { //如果是当前目录或父目录,跳过 if ( ! ( strcmp( FindFileData.cFileName, "." ) ) || ! ( strcmp( FindFileData.cFileName, ".." ) ) ) { continue; } //恢复初始用户选择目录 strcpy( path, pathbak ); //列出所有发现的文件 sprintf( path, "%s//%s", path, FindFileData.cFileName ); //如果SetCurrentDirectory成功的话,则它是一个目录,递归调用继续搜索子目录 if ( ( SetCurrentDirectory( path ) ) ) { SearchFolder( path ); } //插入文件及路径名到列表框m_listbox_hwnd中 SendMessage( m_listbox_hwnd, LB_ADDSTRING, 0, path ); //<--INSERT WHAT YOU WANT DONE HERE! } } while ( FindNextFile ( hFind, &FindFileData ) && hFind != INVALID_HANDLE_VALUE ); FindClose ( hFind ); }
CSDN VC编程经验总结
|