我有这个代码,它抓取某个目录中的文件。我想按时过滤这些文件。我希望文件类型过滤20秒或更短。如何将此过滤器添加到我的代码中?谢谢。
using namespace std;
typedef vector<WIN32_FIND_DATA> tFoundFilesVector;
std::wstring LastWriteTime;
int getFileList(const char * filespec, tFoundFilesVector &foundFiles)
{
WIN32_FIND_DATA findData;
HANDLE h;
int validResult=true;
int numFoundFiles = 0;
h = FindFirstFile((LPCSTR)filespec, &findData); //ansi
if (h == INVALID_HANDLE_VALUE)
return 0;
while (validResult)
{
numFoundFiles++;
foundFiles.push_back(findData);
validResult = FindNextFile(h, &findData);
}
return numFoundFiles;
}
void showFileAge(tFoundFilesVector &fileList)
{
unsigned _int64 fileTime, curTime, age;
tFoundFilesVector::iterator iter;
FILETIME ftNow;
CoFileTimeNow(&ftNow);
curTime = ((_int64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime;
for (iter=fileList.begin(); iter<fileList.end(); iter++)
{
fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;
age = curTime - fileTime;
wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << " seconds" << endl;
}
}
int main()
{
string fileSpec = "*.*";
tFoundFilesVector foundFiles;
tFoundFilesVector::iterator iter;
int foundCount = 0;
getFileList("c:\\Mapper\\*.txt", foundFiles);
getFileList("c:\\Mapper\\*.jpg", foundFiles);
foundCount = foundFiles.size();
if (foundCount)
{
wcout << "Found "<<foundCount<<" matching files.\n";
showFileAge(foundFiles);
}
system("pause");
return 0;
}
答案 0 :(得分:1)
你去吧。我更改了FindFile调用以显式使用“A”版本(并明确使用WIN32_FIND_DATAA而不是WIN32_FIND_DATA)。对LPCSTR的投射看起来很可疑,但我不知道你是否正在使用unicode进行编译(默认情况下Visual Studio默认为Unicde“W”apis)。
另外,你没有调用FindClose,我补充说。否则,我只使用GetSystemTime获取当前时间,将其转换为文件时间,然后将文件时间引用为64位整数。 “unsigned long long”与unsigned __int64相同。
using namespace std;
typedef vector<WIN32_FIND_DATAA> tFoundFilesVector;
std::wstring LastWriteTime;
unsigned long long FileTimeToULL(const FILETIME& ft)
{
unsigned long long ull;
ull = ft.dwLowDateTime | (((unsigned long long)ft.dwHighDateTime) << 32);
return ull;
}
int getFileList(const char * filespec, tFoundFilesVector &foundFiles, DWORD dwMaxAgeInSeconds)
{
WIN32_FIND_DATAA findData={};
HANDLE h;
int validResult=true;
int numFoundFiles = 0;
unsigned long long now = 0;
unsigned long long age = 0;
SYSTEMTIME stnow = {};
FILETIME ftnow = {};
::GetSystemTime(&stnow);
::SystemTimeToFileTime(&stnow, &ftnow);
now = FileTimeToULL(ftnow);
h = FindFirstFileA(filespec, &findData); //ansi
validResult = (h != INVALID_HANDLE_VALUE);
while (validResult)
{
bool fIsDirectory = !!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
age = FileTimeToULL(findData.ftLastWriteTime);
if (age > now)
{
age = 0;
}
else
{
age = now - age;
}
// age is the diff between "right now" and when the file was last touched (in 100ns units)
// convert to seconds
age /= 10000000;
if ((age <= dwMaxAgeInSeconds) && (!fIsDirectory))
{
foundFiles.push_back(findData);
numFoundFiles++;
}
validResult = FindNextFileA(h, &findData);
}
if (h != INVALID_HANDLE_VALUE)
{
FindClose(h);
}
return numFoundFiles;
}