VC递归获取指定目录下的所有文件名字及路径

6562阅读 0评论2012-09-04 renxiaobin32132
分类:WINDOWS

经测试有效。

#include
#include
using namespace std;

//获取指定目录下的文件列表
/* strDir=D:\\t0506\\ */
BOOL getfilelist(CString strSourceDir, CString fileListPath)
{
//保存文件列表
CString strFileList = "";

//要查找的目录
strSourceDir = strSourceDir + "*.*";

_finddata_t file;
long longf;

if((longf = _findfirst(strSourceDir, &file))==-1l)
{
return FALSE;
}
else
{
string tempName;

while( _findnext(longf, &file ) == 0)
{
tempName = "";
tempName = file.name;
if (tempName == "..")
{
continue;
}

//保存文件名
strFileList.Insert(strFileList.GetLength(),file.name);
strFileList.Insert(strFileList.GetLength(), "\r\n");
}
}

_findclose(longf);

CFile txtFile; //文件列表写TXT文件
txtFile.Open(fileListPath,CFile::modeCreate|CFile::modeWrite);
txtFile.Write(strFileList,strFileList.GetLength());
txtFile.Close();

return TRUE;
}

//测试获取文件列表的函数.在工程目录下会自动生成list.txt。
void CTrdDlg::OnButton1()
{
getfilelist("D:\\t0506\\", "list.txt");
}

上一篇:c字符串处理函数
下一篇:matlab 获取当前文件所在路径的上一级路径