#include "stdafx.h"
#include "windows.h"
#include <vector>
#include <string>
#include "iostream"
using namespace std;
typedef std::vector<std::string> file_lists;
static int str_compare(const void *arg1, const void *arg2)
{
//比较字符串arg1 and arg2
return strcmp((*(std::string*)arg1).c_str(), (*(std::string*)arg2).c_str());
}
file_lists ScanDirectory(const std::string &path, const std::string &extension)
{
//WIN32_FIND_DATA:Contains information about the file that is found
//by the FindFirstFile,FindFirstFileEx, or FindNextFile function
WIN32_FIND_DATA wfd;
HANDLE hHandle;
string searchPath, searchFile;
file_lists vFilenames;
int nbFiles = 0;
searchPath = path + "/*" + extension;
//Searches a directory for a file or subdirectory with a name that matches a specific name
hHandle = FindFirstFile(searchPath.c_str(), &wfd);
if (INVALID_HANDLE_VALUE == hHandle)
{
fprintf(stderr, "ERROR(%s, %d): Cannot find (*.%s)files in directory %s/n", __FILE__, __LINE__, extension.c_str(), path.c_str());
exit(0);
}
do
{
//. or ..
if (wfd.cFileName[0] == ‘.‘)
{
continue;
}
// if exists sub-directory
//dwFileAttributes:The file attributes of a file
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//FILE_ATTRIBUTE_DIRECTORY:The handle identifies a directory
continue;
}
else //if file
{
searchFile = path + "/" + wfd.cFileName;
vFilenames.push_back(searchFile);
nbFiles++;
}
}while (FindNextFile(hHandle, &wfd));
//Call this member function to continue a file search begun with a call to CGopherFileFind::FindFile
FindClose(hHandle);
//Closes a file search handle opened by the FindFirstFile,FindFirstFileEx,or FindFirstStreamW function
// sort the filenames,Performs a quick sort
qsort((void *)&(vFilenames[0]), (size_t)nbFiles, sizeof(string), str_compare);
return vFilenames;
}
int main(int argc, char* argv[])
{
file_lists files = ScanDirectory("E:", ".jpg");
if (files.empty())
{
cout<<"no image file find in current directory.."<<endl;
system("pause");
exit(-1);
}
int size = files.size();
cout<<"there are "<<size<<" image files totally...."<<endl;
for (int i=0; i<size; i++)
{
cout<<files[i].c_str()<<endl;
}
system("pause");
return 0;
}
//上述是在VS2010环境中编译,运行会在E:/目录下寻找所有后缀为.jpg的文件并打印出来
//#include "stdafx.h"是Visual Studio工具的一个预编译软件。如果你不是用vs工具,或者,你在VS里面没有设置预编译选项,则不需要这一行。
//其次在VS的工程属性里面,不要用unicode字符,改为no set.
//以下是在matlab中的遍历读取文件目录的功能函数
function fileList = readFileListFromFolder(folderName, fileSuffix)
% Read all the files with special suffix to a file
% list.
% A simple use case:
% fileList = readFileListFromFolder(‘test\‘, ‘txt‘);
%Check the input folderName
if ~isempty(findstr(folderName,‘ ‘))
disp(‘There are some space in the folder name: ‘);
disp([‘ ‘folderName]);
disp(‘Please change to use the version2 instead of this one!‘);
end
if strcmp(folderName(end), ‘\‘)
[s, w] = dos([‘dir‘ folderName ‘*.‘ fileSuffix ‘/s /B >fileList.txt‘]);
else
[s, w] = dos([‘dir‘ folderName ‘\*.‘ fileSuffix ‘/s /B >fileList.txt‘]);
end
fileList = importdata(‘fileList.txt‘);
dos(‘del fileList.txt‘);
end