zoukankan      html  css  js  c++  java
  • 搜索指定目录下的所有文件或者指定文件(可用于多级目录)

    #include<windows.h>
    #include<iostream>
    #include<string>
    using namespace std;
    
    //只能处理目录:lpPath只能是路径
    void find(char *lpPath)
    {
        char szFind[MAX_PATH];
       char szFile[MAX_PATH];
    
        WIN32_FIND_DATA FindFileData;
    
        strcpy(szFind,lpPath);
        strcat(szFind,"//*.*");
    
        HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
        if(INVALID_HANDLE_VALUE == hFind)    return;
        while(TRUE)
        {
            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if(FindFileData.cFileName[0]!='.')
                {
                    strcpy(szFile,lpPath);
                    strcat(szFile,"//");
                    strcat(szFile,FindFileData.cFileName);
                    find(szFile);
                }
            }
            else
            {      
                   cout<<FindFileData.cFileName<<endl;
            }
            if(!FindNextFile(hFind,&FindFileData))
                break;
        }
        FindClose(hFind); 
    }
    //可同时处理目录和文件:path可以是路径,也可以是文件名,或者文件通配符
    void _find(string path){ 
         //取路径名最后一个"//"之前的部分,包括"//"
        string prefix=path.substr(0,path.find_last_of('//')+1);
    
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind=::FindFirstFile(path.c_str(), &FindFileData);
        if(INVALID_HANDLE_VALUE == hFind)
        {
           cout<<"文件通配符错误"<<endl;
          return;
       }
        while(TRUE)
        {
          //目录
            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                //不是当前目录,也不是父目录
                if(FindFileData.cFileName[0]!='.')
                {
                    //查找下一级目录
                    _find(prefix + FindFileData.cFileName + "//" + "*.*");
                }
            }
            //文件
            else
            {   
                 cout<<FindFileData.cFileName<<endl;
            }
            if(!FindNextFile(hFind,&FindFileData))
                  break;
        }
        FindClose(hFind); 
    }
    
    void main(){
       // find("E:");//目录:E盘
        _find("E://*.*");//E盘下所有文件
    
       // string str="./test"; //这里指定的是目录
    
        //string path;
        //cout<<"请输入文件通配符:"<<flush;
        //cin>>path;
        // str=str+path;
       // find((char*)str.c_str());//可以处理".",".." 不可以处理"*","..//*" 
        //_find(str);//可以处理"*","..//*" 不可以处理".",".."
        system("pause");
    }

  • 相关阅读:
    Vue-router(5)之 路由的before家族
    Vue-router(4)之路由跳转
    Vue-router(3)之 router-link 和 router-view 使用
    Vue-router(1)之component标签
    Vue.js(4)- 生命周期
    Vue.js 之 过渡动画
    Vue.js(2)- 过滤器
    函数节流和函数防抖
    认识与学习 BASH
    linux下创建文件与目录时默认被赋予了什么样的权限?
  • 原文地址:https://www.cnblogs.com/SunkingYang/p/11049226.html
Copyright © 2011-2022 走看看