struct Cfile_Path_size {
CString strFileName;
double size;
};
void GetAllFilesPath(const std::string path, vector<CString>& files)
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo; //很少用的文件信息读取结构
std::string p;
std::string strDir = path;
if (strDir.at(strDir.length() - 1) != '\\')
{
strDir += "\\";
}
p = strDir + "*";
if ((hFile = _findfirst(p.c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR)) //判断是否为文件夹
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
p = strDir + fileinfo.name;
GetAllFilesPath(p, files);//递归当前文件夹
}
}
else //文件处理
{
CStringA strA = strDir.c_str();
strA += fileinfo.name;
CStringW strW;
strW = strA;
files.push_back(strW);//文件名
}
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
}
void main()
{
std::string Path = "D:\\Cloner";
vector<CString> files;
Cfile_Path_size Cfile;
vector<Cfile_Path_size> vecfileAll;
GetAllFilesPath(Path, files);
for (int i = 0; i < files.size(); i++)
{
Cfile.strFileName = files[i];
HANDLE fhadle = CreateFile(files[i], 0, 0, 0, OPEN_EXISTING, 0, 0);
Cfile.size = GetFileSize(fhadle,NULL);
vecfileAll.push_back(Cfile);
}
}
|