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;
}