What Time Is This Returning

2019-08-21 18:32发布

Deep in the sauce here. I haven't worked with time to much so I'm a little confused here. I know there is FILETIME and SYSTEMTIME. What I am trying to get at this point (because it might change) are file that are less than a 20 seconds old. This returning the files and their size and something in seconds, What I'd like to know is where it is filtering by time if it is, and how can I adjust it to suit my needs. Thank you.

using namespace std;
typedef vector<WIN32_FIND_DATA> tFoundFilesVector;
std::wstring LastWriteTime;  
int getFileList(wstring filespec, tFoundFilesVector &foundFiles)
{
    WIN32_FIND_DATA findData;
    HANDLE h;
    int validResult=true;

    int numFoundFiles = 0;
    h = FindFirstFile(filespec.c_str(), &findData);

    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;
    //__int64 nFileSize;
          //LARGE_INTEGER li;    
    //li.LowPart = ftNow.dwLowDateTime;
    //li.HighPart = ftNow.dwHighDateTime;

    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;

        cout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl;
    }
}

int main()
{
    string fileSpec = "*.*";
    tFoundFilesVector foundFiles;
    tFoundFilesVector::iterator iter;

    int foundCount = 0;

    getFileList(L"c:\\Mapper\\*.txt", foundFiles);
    getFileList(L"c:\\Mapper\\*.jpg", foundFiles);

    foundCount = foundFiles.size();
    if (foundCount)
    {
        cout << "Found "<<foundCount<<" matching files.\n";
        showFileAge(foundFiles);
    }
    system("pause");
    return 0;
}

2条回答
smile是对你的礼貌
2楼-- · 2019-08-21 19:05

I don't know what you've done to try to debug this but your code doesn't work at all. The reason is you're passing getFileList() a wstring but then passing that to the ANSI version of FindFirstFile(). Unless you #define UNICODE or use the appropriate compiler option, all system calls will expect char *, not UNICODE.

The easiest fix is to simply change the declaration of getFileList() to this:

int getFileList(const char * filespec, tFoundFilesVector &foundFiles)

Change the call to FindFirstFile() to this:

h = FindFirstFile((LPCSTR)filespec, &findData);

And then change the calls to it to this:

getFileList("c:\\Mapper\\*.txt", foundFiles);
getFileList("c:\\Mapper\\*.jpg", foundFiles);

Your other option is to switch all char strings to wide chars, but either way you need to be consistent throughout. Once you do that the program works as expected.

As for your final question, your program is not filtering by time at all.

查看更多
祖国的老花朵
3楼-- · 2019-08-21 19:07

Not quite an answer, but you might want to read about file system tunneling.

It may prevent you from what you're trying to do in some situations.

查看更多
登录 后发表回答