Skip to content

Instantly share code, notes, and snippets.

@huoxudong125
Created June 4, 2014 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huoxudong125/ed43a48fa3a981ce43fd to your computer and use it in GitHub Desktop.
Save huoxudong125/ed43a48fa3a981ce43fd to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <string>
#include <vector>
#include <stack>
#include <iostream>
#include <map>
#include <regex>
using namespace std;
bool ListFiles(wstring path, wstring mask, vector<wstring>& files) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
stack<wstring> directories;
directories.push(path);
files.clear();
while (!directories.empty()) {
path = directories.top();
spec = path + L"\\" + mask;
directories.pop();
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
if (wcscmp(ffd.cFileName, L".") != 0 &&
wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push(path + L"\\" + ffd.cFileName);
}
else {
files.push_back(path + L"\\" + ffd.cFileName);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;
}
bool ListFiles(wstring path, wstring mask, map<int,wstring>& files) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
stack<wstring> directories;
directories.push(path);
files.clear();
while (!directories.empty()) {
path = directories.top();
spec = path + L"\\" + mask;
directories.pop();
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
if (wcscmp(ffd.cFileName, L".") != 0 &&
wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push(path + L"\\" + ffd.cFileName);
}
else {
try {
wsmatch wideMatch;
wregex fileRegex(L"^.*?(\\d{1,8})\\.tiff?$");//(^.*\\D)?\\d{1,8}.tiff$");
wstring target(ffd.cFileName);
if (regex_match(target.cbegin(), target.cend(), wideMatch, fileRegex))
{
std::wstring fileSeq = wideMatch[1].str();
cout << stoi(fileSeq) << endl;
}
else
{
wcout <<"can't match :"<< wideMatch.str() << endl;
}
files.insert({ 0, path + L"\\" + ffd.cFileName });
}
catch (std::regex_error& e) {
if (e.code() == std::regex_constants::error_badrepeat)
std::cerr << "Repeat was not preceded by a valid regular expression.\n";
else if (e.code() == std::regex_constants::error_syntax)
std::cerr << "syntax error" << endl;
else std::cerr << "Some other regex exception happened.\n";
}
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;
}
int main(int argc, char* argv[])
{
/*vector<wstring> files;
if (ListFiles(L"F:", L"*", files)) {
for (vector<wstring>::iterator it = files.begin();
it != files.end();
++it) {
wcout << it->c_str() << endl;
}
}*/
map<int, wstring> files;
if (ListFiles(L"E:\\Raw_Lw3\\LW3-1-4-2mm", L"*", files))
{
}
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment