开发者

Adding a Filter [duplicate]

开发者 https://www.devze.com 2023-04-09 20:52 出处:网络
This question already has answers here: What Time Is This Returning (2 answers) Closed 8 years ago. I have this code that is grabbing files in a certain directory.I would like to filter the
This question already has answers here: What Time Is This Returning (2 answers) Closed 8 years ago.

I have this code that is grabbing files in a certain directory. I would like to filter these files by time. I want the file types to be filtered by 20 seconds or less. How can I add this filter to my code? Thank you.

using namespace std; 
typedef vector<WIN32_FIND_DATA> tFoundFilesVector; 
std::wstring LastWriteTime;   
int getFileList(const char * filespec, tFoundFilesVector &foundFiles)
{ 
    WIN32_FIND_DATA findData; 
    HANDL开发者_JAVA技巧E 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; 
}


Here your go. I changed the FindFile calls to explicitly use the "A" version (and explicitly use WIN32_FIND_DATAA instead of WIN32_FIND_DATA). That cast to LPCSTR looked suspicious, but I didn't know if you were compiling with unicode on or off (Visual Studio defaults to Unicde "W" apis by default).

Also, you weren't calling FindClose, and I added that. Otherwise, I just use GetSystemTime to get the current time, convert it to file time, and then reference file times as 64-bit ints. "unsigned long long" is the same as 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; 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消