C++怎么样能列出文件夹里的指定后缀名的文件

li08240418 2011-12-21 02:29:04
求 教育!
...全文
258 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-12-21
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt");
//读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字
system("dir /b /ad c:\\*.* >d:\\alldirs.txt");
//读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字
请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
li08240418 2011-12-21
  • 打赏
  • 举报
回复
谢谢 如果不用CFileFind finder;
CString Finddir呢
wodeprogrammer 2011-12-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 woshic__ 的回复:]
#include "afx.h"
void FindeFile(char *pFilePath)
{
CFileFind finder;
CString Finddir;
Finddir.Format("%s\\*.exe",pFilePath);

BOOL ret = finder.FindFile(Finddir);
while (ret)
{
ret = finder.……
[/Quote]
windows下实现的方法,确实不错
woshic__ 2011-12-21
  • 打赏
  • 举报
回复
#include "afx.h"
void FindeFile(char *pFilePath)
{
CFileFind finder;
CString Finddir;
Finddir.Format("%s\\*.exe",pFilePath);

BOOL ret = finder.FindFile(Finddir);
while (ret)
{
ret = finder.FindNextFile();
CString strPath = finder.GetFilePath();
if(finder.IsDirectory())
{
char *temp =strPath.GetBuffer(0);
temp+=strPath.GetLength();
while(*temp!='\\')
{
temp--;
}
CString Filename = temp;
if(Filename=="\\."||Filename=="\\..")
continue;//如果是目录不处理
else
{
FindeFile(strPath.GetBuffer(0));
TRACE("目录:%s\r\n",strPath);
}
}
else
{
TRACE("文件:%s\r\n",strPath);
}
}
finder.Close();
}
li08240418 2011-12-21
  • 打赏
  • 举报
回复
具体点啊
woshic__ 2011-12-21
  • 打赏
  • 举报
回复
遍历文件夹

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧