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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值