0
votes

I am working on a script that will delete files from a folder that was modified over a week ago and i am having trouble with getting the last modified date into a string.

So I am trying to write the last modified date of a file to a string vector.

The bolded line is the error line, it says

||=== Build: Debug in WeekaDelete (compiler: GNU GCC Compiler) ===| \WeekaDelete\main.cpp||In function 'int main(int, char**)':| \WeekaDelete\main.cpp|21|error: cannot bind 'std::ostream {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&'| codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream|602|error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits; _Tp = _FILETIME]'| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

#include <windows.h>
#include <vector>
#include <ctime>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
    WIN32_FIND_DATA search_data;
    memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
    HANDLE handle = FindFirstFile("C:\\Users\\Meikle-John\\Desktop\\CoastWideCivil\\C++\\Scans\\*",         &search_data);
    int ifilecount = -2;
    vector<string> vsname, vsdate;
    string tempn, tempd;
    while(handle != INVALID_HANDLE_VALUE)
    {
        tempn = search_data.cFileName;
        **tempd = search_data.ftLastAccessTime;**
        cout << tempd << endl;
        cout << tempn << " : " << tempd << endl;
        cout << ifilecount++ << endl;
        if(ifilecount > -1)
        {
            vsname.push_back(tempn);
            vsdate.push_back(tempd);
        }
        if(FindNextFile(handle, &search_data) == FALSE)
        {
            break;
        }
    }
    //Close the handle after use or memory/resource leak
    FindClose(handle);
    cout << "There are:" << ifilecount << " Files in this directory" << endl;
    return 0;
}
2

2 Answers

2
votes

Since you're using Win32 the easiest thing would be to use the GetDateFormat function:

TCHAR tchDate[80];

SYSTEMTIME st;
FileTimeToSystemTime(&search_data.ftLastAccessTime, &st);

GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE,
    &st, nullptr, tchDate, _countof(tchDate));
cout << tchDate;

There is also GetTimeFormat if you want the time as well as the date.

0
votes

You're trying to assign to a std::string from a FILETIME structure. The C++ Standard library doesn't know how you want this MS Windows type to be output, and Microsoft don't bother to provide a convenient streaming function in their header... you have to find and use a Windows function to get a textual representation. See Mr Potter's answer for that....