VC获取文件夹内指定类型的文件名

VC C++获取文件夹内指定类型的文件名

#include <Windows.h>
#include <iostream>
#include <string>
#include <io.h>
#include <shlwapi.h>

#pragma comment(lib,"shlwapi.lib")

using namespace std;

void GetFileNameInOneDir(string Path, string Type)
{
_finddata_t file;
long longf;


string PathTemp=Path;
PathTemp+="\\*.*";
string FullPath=Path;
FullPath+="\\";


if((longf = _findfirst(PathTemp.c_str(), &file))==-1l)
{
cout<<"文件没有找到!"<<endl;
}
else
{
string tempName;
while( _findnext( longf, &file ) == 0 )
{
tempName = "";
tempName = file.name;
if (tempName == "..")
{
continue;
}
FullPath+=file.name;
if(strcmp(Type.c_str(),PathFindExtension(FullPath.c_str()))==0)
cout<<file.name<<endl;
}
}

_findclose(longf);
}
void main(int argc, char **argv)
{
GetFileNameInOneDir("E:\\tt",".txt");
}


VC C++获取文件夹内指定类型的文件名

用链表保存查询到的文件名

#include <Windows.h>
#include <iostream>
#include <string>
#include <io.h>
#include <shlwapi.h>
#include <list>

#pragma comment(lib,"shlwapi.lib")
using namespace std;


list<string> GetFileNameInOneDir(string Path, string Type)
{
list<string> ListName;
_finddata_t file;
long longf;


string PathTemp=Path;
PathTemp+="\\*.*";


string FullPath=Path;
FullPath+="\\";


if((longf = _findfirst(PathTemp.c_str(), &file))==-1l)
{
cout<<"文件夹没有找到!"<<endl;
}
else
{
string tempName;
while( _findnext( longf, &file ) == 0 )
{
tempName = "";
tempName = file.name;
if (tempName == "..")
{
continue;
}
FullPath+=file.name;
if(strcmp(Type.c_str(),PathFindExtension(FullPath.c_str()))==0)
ListName.push_back(file.name);
}
}

_findclose(longf);


return ListName;
}
void main(int argc, char **argv)
{
list<string> allName=GetFileNameInOneDir("E:\\Linux\\新資料夾\\小说",".txt");
list<string>::iterator listElement;


allName.sort();


for(listElement=allName.begin();listElement!=allName.end();++listElement)
cout<<*listElement<<endl;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值