怎样枚举文件夹下的所有文件 findfirstfile+findnextfile+递归,这样,okokok 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 去鸡丁blog上看,那上面有他实现的一个枚举文件的迭代器,可以和stl算法一起使用。。还有就是用上面几楼说的方法,用boost也可以。。 使用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;} 老衲给你代码,你直接调用传递参数就可以了#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("文件夹路径"); 刷新闪烁的问题 数据库排序问题,帮帮小妹吧,刚接触 高手指教,如何用vc写一程序,使之不能在进程中看到呢? Lnk2005的连接错误怎样解决!!!在先急等。。。。 谁能给我讲讲VC??? SOS!!!如何获取用户打开的路径!! char用什么方法可以转变成unsigned char(串口) 关于CEditView的字体显示 对bmp32位色彩位图操作 关于报表打印的问题 用tao_idl 编译Cosnaming.idl 无法定位程序输入点于ACEd.dll 求助:VC++6.0中new的BUG问题的解决方案?
还有就是用上面几楼说的方法,用boost也可以。。
#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;
}
老衲给你代码,你直接调用传递参数就可以了#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("文件夹路径");