获取路径下所有文件

来源:互联网 发布:下载优化清理大师 编辑:程序博客网 时间:2024/05/20 04:29
#include <iostream>#include <stdlib.h>#include <Windows.h>#include <string>#include <vector>#include <io.h>using namespace std;void GetAllFiles(string path, vector<string>& files){long hfile = 0;struct _finddata_t fileinfo;string p;if ((hfile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) == NO_ERROR);{do{if ((fileinfo.attrib & _A_SUBDIR)){if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != NO_ERROR)GetAllFiles(p.assign(path).append("/").append(fileinfo.name), files);}else{files.push_back(p.assign(path).append("/").append(fileinfo.name));}} while (_findnext(hfile, &fileinfo) == NO_ERROR);_findclose(hfile);}}int main(){vector<string> files;string filebuf;char modulbuf[FILENAME_MAX];GetModuleFileName(NULL, modulbuf, FILENAME_MAX);filebuf.append(modulbuf);string path(filebuf.substr(0, filebuf.find_last_of("\\")));GetAllFiles(path, files);for (vector<string>::iterator it = files.begin(); it != files.end(); it++){cout << it->c_str() << endl;}return 0;}


Example:


另外

/* File attribute constants for _findfirst() */#define _A_NORMAL       0x00    /* Normal file - No read/write restrictions */#define _A_RDONLY       0x01    /* Read only file */#define _A_HIDDEN       0x02    /* Hidden file */#define _A_SYSTEM       0x04    /* System file */#define _A_SUBDIR       0x10    /* Subdirectory */#define _A_ARCH         0x20    /* Archive file */

_A_HIDDEN
可以用于查看隐藏属性文件。