linux文件夹遍历按时间,Window&Linux遍历某一文件夹

#include #include#include

//#define linux//#define _WIN32

using namespacestd;

vector getFilesList(stringdir);intmain()

{char dir[200];

cout<< "Enter a directory:";

cin.getline(dir,200);

vectorallFileList =getFilesList(dir);

cout<< "输出所有文件的路径:" <

{string filePath =allFileList.at(i);

cout<< filePath <

}return 0;

}

#ifdef linux

#include#includevector getFilesList(stringdirpath) {

vectorallPath;

DIR*dir =opendir(dirpath.c_str());if (dir ==NULL)

{

cout<< "opendir error" <

}struct dirent *entry;while ((entry = readdir(dir)) !=NULL)

{if (entry->d_type == DT_DIR) {//It's dir

if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)continue;string dirNew = dirpath + "/" + entry->d_name;

vector tempPath =getFilesList(dirNew);

allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());

}else{//cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;

string name = entry->d_name;string imgdir = dirpath + "/" +name;//sprintf("%s",imgdir.c_str());

allPath.push_back(imgdir);

}

}

closedir(dir);//system("pause");

returnallPath;

}#endif#ifdef _WIN32//__WINDOWS_

#include vector getFilesList(stringdir)

{

vectorallPath;//在目录后面加上"\*.*"进行第一次搜索

string dir2 = dir + "\*.*";

intptr_t handle;

_finddata_t findData;

handle= _findfirst(dir2.c_str(), &findData);if (handle == -1) {//检查是否成功

cout << "can not found the file ..." <

}while (_findnext(handle, &findData) == 0)

{if (findData.attrib & _A_SUBDIR) 是否含有子目录

{//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索

if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)continue;//在目录后面加上"\"和搜索到的目录名进行下一次搜索

string dirNew = dir + "\" +findData.name;

vector tempPath =getFilesList(dirNew);

allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());

}else //不是子目录,即是文件,则输出文件名和文件的大小

{string filePath = dir + "\" +findData.name;

allPath.push_back(filePath);

}

}

_findclose(handle);//关闭搜索句柄

returnallPath;

}#endif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值