Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok, I have this code that is giving me the number of files in a directory and it's working just skippy. The problem I now have and I'm hopeing it's my last one is that I want only the files that are less than a minute old. Just how do I add that filter to this code? Any suggestions are welcome. Thank you so very much everyone.

C++
struct file_data 
{ 
    std::wstring sLastAccessTime; 
    __int64 nFileSize      ; 
}; 
 
int GetFileList(const wchar_t *searchkey, std::map &map) 
{ 
    WIN32_FIND_DATA fd; 
    HANDLE h = FindFirstFile(searchkey,&fd); 
    if(h == INVALID_HANDLE_VALUE) 
    { 
        return 0; // no files found 
    } 
    while(1) 
    { 
        wchar_t buf[128]; 
        FILETIME ft = fd.ftLastWriteTime; 
        SYSTEMTIME sysTime; 
        FileTimeToSystemTime(&ft, &sysTime); 
        wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay); 
 
        file_data filedata; 
        filedata.sLastAccessTime= buf; 
        filedata.nFileSize      = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 
 
        map[fd.cFileName]= filedata; 
 
        if (FindNextFile(h, &fd) == FALSE) 
            break; 
    } 
    return map.size(); 
}
Posted

1 solution

Come on dude, your code has
FILETIME ft = fd.ftLastWriteTime;
SYSTEMTIME sysTime;
FileTimeToSystemTime(&ft, &sysTime);
already and you're asking where to put time checks?

Google / Bing / Yahoo Search for SYSTEMTIME, FILETIME and you'll find tons of articles on how to do arithmetic on those values. Hint,
C#
#define ONEDAY    ((__int64)(__int64)10000000*(__int64)(60*60*24))
#define ONEHOUR   ((__int64)(__int64)10000000*(__int64)(60*60))
#define ONEMINUTE ((__int64)(__int64)10000000*(__int64)(60))
will help you with the math and what the units are from your research into how files record times.
 
Share this answer
 
Comments
Member 7766180 30-Sep-11 0:34am    
Ok thats fair. I'm off and running! Thank you.
Sergey Alexandrovich Kryukov 30-Sep-11 1:11am    
Here we go. My 5.
--SA
Chuck O'Toole 30-Sep-11 1:42am    
thanks. And yes, here we go again.
Member 7766180 30-Sep-11 1:36am    
Ok SA and Chuck. Would I subtract 60 * 1000 * 10000LL from FILETIME?
FileTimeToSystemTime(&ft-60 * 1000 * 10000LL, &sysTime);
Chuck O'Toole 30-Sep-11 1:40am    
imprecise question. FILETIME is a data type, you might as well ask "should I subtract .... from 'int'. What 'data item' do you want to subtract from? And if you do identify what you want to subtract from, what do you think that will give you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900