如何用mfc访问一个文件夹,并存储文件夹内所有文件的路径

如何用mfc访问一个文件夹,并存储文件夹内所有文件的路径,第1张

CFileFind finder

if(finder.IsDirectory() &&!finder.IsDots())

{

// 是子文件夹,递归调用本函数

}

else

{

finder.GetFilePath()// 获取文件的完整路径

finder.GetFileName()// 获取文件名,包括扩展名

}

CFileFind find

CString Path = lpszPath

CString lpsz = Path +L"\\"

Path = Path +L"\\*.*"

BOOL IsFind = find.FindFile(Path)

while(IsFind )

{

IsFind=find.FindNextFile()

//如果是"."则不扫描

if(find.IsDots())

continue

//是目录,继续扫描此目录

else if(find.IsDirectory())

{

CString strPath = lpszPath

strPath = strPath + L"\\" + find.GetFileName()

ScanDiskFile(strPath)

}

//文件

else

{

//获得文件的路径

m_strFile = find.GetFileName()

CString extend = m_strFile.Right(m_strFile.GetLength() - m_strFile.ReverseFind('.') - 1)//取得扩展名

if (extend == m_ext_one | extend == m_ext_two)//m_ext_now为你要查找的文件扩展名

{

m_strArray.Add(lpsz + m_strFile)

}

}

}

find.Close()

m_ext_one、m_ext_two用于指定需要搜索的文件后缀名

'm_File' : undeclared identifier ,没有这个变量,要先定义的类型的;

m_File 在类 .h 文件里面定义, CString m_File

或者直接在函数里面定义也可以


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/11490795.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存