1. 程式人生 > 其它 >檔案遍歷(只適用windows)

檔案遍歷(只適用windows)

技術標籤:C++

#include <io.h>
void getAllFiles(string path, vector<string>& files, string format)
{
 intptr_t  hFile = 0;//檔案控制代碼  
 struct _finddata_t fileinfo;//檔案資訊  
 string p;
 if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) //檔案存在 
{ do { if ((fileinfo.attrib & _A_SUBDIR))//判斷是否為資料夾 { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)//資料夾名中不含"."和".." { files.push_back(p.assign(path).append("\\").append(fileinfo.name))
; //儲存資料夾名 getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files, format); //遞迴遍歷資料夾 } } else { files.push_back(p.assign(path).append("\\").append(fileinfo.name));//如果不是資料夾,儲存檔名 } } while (_findnext(hFile, &fileinfo) == 0); _findclose
(hFile); } } int main() { string filePath = "C:\\Users\\admin\\Desktop\\廣州"; vector<string> files; string format = ".bmp"; //查詢檔案的格式 getAllFiles(filePath, files, format); int size = files.size(); }