findfirstfile+findnextfile+递归,这样,okokok

解决方案 »

  1.   

    去鸡丁blog上看,那上面有他实现的一个枚举文件的迭代器,可以和stl算法一起使用。。
    还有就是用上面几楼说的方法,用boost也可以。。
      

  2.   

    使用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;
    }
      

  3.   


    老衲给你代码,你直接调用传递参数就可以了#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("文件夹路径");