Sunday, 15 December 2019

Last modified date of a file into a string c++



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 

#include
#include
#include
#include
#include

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 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;
}

Answer




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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...