#include "Traversal.h"
#include <shtypes.h>
#include <algorithm>
int Traversal(string& inputPath, vector<string>& fileName, const string& fileType)
{
string findFile = inputPath + "\\*";
WIN32_FIND_DATAA findFileData;
HANDLE handle = ::FindFirstFileA(findFile.c_str(), &findFileData);
if(INVALID_HANDLE_VALUE == handle)
{
return -1;
}
do
{
string name = findFileData.cFileName;
string fullName = inputPath + "\\" + name;
if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // FindFileData为目录
{
if(findFileData.cFileName[0] != '.') // 排除. 和..
{
Traversal(fullName, fileName, fileType);
}
}
else // FindFileData为文件名
{
if (fileType.size() == 0)
{
fileName.push_back(fullName);
}
else
{
int pos = name.find_last_of('.');
if (pos > 0)
{
string suffix = name.substr(pos+1);
transform(suffix.begin(), suffix.end(), suffix.begin(), tolower);
if(fileType == suffix) // 后缀名符合
{
fileName.push_back(fullName);
}
}
}
}
} while(FindNextFileA(handle, &findFileData));
FindClose(handle);
return 0;
}