怎样枚举文件夹下的所有文件

jasonsan 2007-05-14 09:16:07
请教大家:如何用c++实现枚举一个文件夹下的所以文件,
...全文
1571 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sksgod 2009-01-12
  • 打赏
  • 举报
回复

老衲给你代码,你直接调用传递参数就可以了

#include "windows.h"
#include "stdio.h"
#include <Shlobj.h>

void delallfile(char *Path)
{
char file[MAX_PATH];
lstrcpy(file,Path);
lstrcat(file,"\\*.*");
WIN32_FIND_DATA wfd;
HANDLE Find = FindFirstFile(file,&wfd);
if (Find == INVALID_HANDLE_VALUE)
return;

while (FindNextFile(Find, &wfd))
{
if (wfd.cFileName[0] == '.')
{
continue;
}
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
char szFindPath[MAX_PATH];
lstrcpy(szFindPath,Path);
lstrcat(szFindPath,"\\");
lstrcat(szFindPath,wfd.cFileName);
delallfile(szFindPath);
}

char FilePath[MAX_PATH];
lstrcpy(FilePath,Path);
lstrcat(FilePath,"\\");
lstrcat(FilePath,wfd.cFileName);
printf("%s\r\n",FilePath);
//DeleteFile(FilePath);
//这里写上你要执行的操作
}
FindClose(Find);


}



主函数调用
delallfile("文件夹路径");

bitxinhai 2009-01-12
  • 打赏
  • 举报
回复
mark
xqhrs232 2009-01-12
  • 打赏
  • 举报
回复
MARK
jasonsan 2007-05-21
  • 打赏
  • 举报
回复
谢谢 搞定了啊
Golden_Tiger 2007-05-20
  • 打赏
  • 举报
回复
有点意思
roger_77 2007-05-17
  • 打赏
  • 举报
回复
使用boost.filesystem

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>


/// 遍历目录strPath中的所有文件
int GetAllFile(const string &strPath)
{
if ( strPath.size() < 2 )
{
return 0;
}
namespace fs = boost::filesystem;
// 得到配置文件夹.
fs::path full_path( fs::initial_path() );
full_path = fs::system_complete( fs::path(strPath, fs::native ) );

unsigned long file_count = 0;
unsigned long dir_count = 0;
unsigned long err_count = 0;

if ( !fs::exists( full_path ) )
{
string strMsg = "找不到文件目录,请检查该目录是否存在:";
strMsg.append(full_path.native_file_string());

return -1;
}

// 遍历指定的文件夹,得到所有的文件名.
if ( fs::is_directory( full_path ) )
{
fs::directory_iterator end_iter;
for ( fs::directory_iterator dir_itr( full_path );
dir_itr != end_iter;
++dir_itr )
{
try
{
if ( fs::is_directory( *dir_itr ) )
{
string strSubDir(full_path.native_directory_string()) ;
strSubDir.append("\\");
strSubDir.append(dir_itr->leaf());
GetAllFile(strSubDir); // 如果有子目录,则递归遍历.
}
else
{
// 先组成包括完整路径的文件名
string strFileName(full_path.native_directory_string());
strFileName.append("\\");
strFileName.append(dir_itr->leaf());
//判断文件是否为XML配置文件
if (false == CheckIsXMLFile(strFileName))
{
continue;
}

fs::path full_file( fs::initial_path() );
full_file = fs::system_complete(fs::path(strFileName, fs::native));

//加载解析文件中的信息.
//readFileInfo(full_file.native_directory_string());
}
}
catch ( const std::exception & ex )
{
++err_count;

}
}//<--for()
loggerXML->debug( "成功遍历所有设备配置文件.");
}
else // must be a file
{
string strMsg = full_path.native_file_string();
strMsg.append(",不是文件目录.");
return -1;
}

return err_count;
}
foochow 2007-05-17
  • 打赏
  • 举报
回复
去鸡丁blog上看,那上面有他实现的一个枚举文件的迭代器,可以和stl算法一起使用。。
还有就是用上面几楼说的方法,用boost也可以。。
jasonsan 2007-05-17
  • 打赏
  • 举报
回复
能不能给点代码啊,刚学的vc++,急用啊。谢谢
sms88 2007-05-14
  • 打赏
  • 举报
回复
FindFirstFile
todototry 2007-05-14
  • 打赏
  • 举报
回复
findfirstfile+findnextfile+递归,这样,okokok
todototry 2007-05-14
  • 打赏
  • 举报
回复
得用api

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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