c/c++实现遍历文件夹下特定的文件

 
 
c++递归遍历文件夹下的所以文件
#include<iostream>
#include<io.h>
using namespace std;

string respath = "*.*";
void displayFile(_finddata_t file,string path){

    string st =path+respath;
    cout<<path;
    const char* p = st.c_str();
    long lf;
    if((lf = _findfirst(p, &file))==-1l)//_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)
        cout<<"文件没有找到!\n";
    else
    {
        cout<<"\n文件列表:\n";
        while( _findnext( lf, &file ) == 0 )//int __cdecl _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1
        {

            string str(file.name);
            int te = str.find(".jsp");
            if(te != -1){
                cout<<file.name<<endl;
            }
            if(file.attrib == _A_NORMAL)cout<<"  普通文件  ";
            else if(file.attrib == _A_RDONLY)cout<<"  只读文件  ";
            else if(file.attrib == _A_HIDDEN )cout<<"  隐藏文件  ";
            else if(file.attrib == _A_SYSTEM )cout<<"  系统文件  ";
            else if(file.attrib == _A_SUBDIR){

                if(strcmp(file.name,"..")==0 || strcmp(file.name,".svn")==0){

                }else{
                    cout<<"  子目录  ";
                    string temp = path;
                    //cout<<file.name<<endl;
                    temp = temp + file.name + "\\";
                    //cout<<temp<<endl;
                    displayFile(file,temp);
                    temp = path;
                }
            }
            else {
               // cout<<"  存档文件  ";
            }
            //cout<<endl;
        }
    }
    _findclose(lf);
}

int main()
{
    string path = "D:\\workspace11\\my-web\\src\\main\\webapp\\";

    _finddata_t file;
    displayFile(file,path);

    return 0;
}


c遍历文件夹所有的以.exe结尾的文件
备注:这个是百度得来的
用shell

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
typedef char* LPCTSTR;

char str[300],ddt[300];
void fileexe(LPCTSTR namepath)
{
     strcat(str,"dir /a ");
     strcat(str,namepath);
     strcat(str,"\\*.exe");
     system(str);
     printf("\n");
     system("pause");
}

int main()
{
    
    printf("Please input the path like (c:\\windows):\n");
    scanf("%s",ddt);
    fileexe(ddt);
    return 0;
}

你可能感兴趣的:(String,struct,File,System,Path,stdstring)