0

I want to retrieve the last write date on a file. I have written this code for it, but it returns me 52428 in values like "Year" all the time

int LastErrorCode;
LPCSTR Path = "C:/Users/Username/Desktop/Picture.PNG";
WIN32_FIND_DATA Information;

if(!FindFirstFile(Path, &Information))
{
    int LastErrorCode = GetLastError();
    cout << "FIND FIRST FILE FAILED" << endl;
    cout << LastErrorCode << endl;
}


SYSTEMTIME MyTime;
FILETIME MyFileTime = Information.ftLastWriteTime;


if(!FileTimeToSystemTime(&MyFileTime, &MyTime))
{
    LastErrorCode = GetLastError();
    cout << "FILE TIME TO SYSTEM TIME FAILED" << endl;
    cout << LastErrorCode << endl;
}


cout << MyTime.wYear << endl;
Janman
  • 698
  • 1
  • 9
  • 25

4 Answers4

6

The hex value for 52428 is 0xCCCC, which seems to indicate it has not been initialized. The function call is probably failing. Check the return codes from FindFirstFile and FileTimeToSystemTime (and then call GetLastError after a failure to find the error code).

Edit Based on the edits to the OP, the FindFirstFile call is likely the one that is failing. The return value is a handle (not a zero/non-zero result). The code should assign the result to a variable of type HANDLE and then compare against INVALID_HANDLE_VALUE.

Note too that after a successful call to FindFirstFile, the code should have a corresponding call to FindClose with the handle to avoid leaking resources.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
  • Hey. The FileTimeToSystemTime failes, and I get the error code 87 which means "The parameter is incorrect.". I have no idea what is wrong. *I have updated the code in the answer – Janman Sep 22 '11 at 16:08
  • @Janman: I added a bit more information. I suspect that your call to FindFirstFile is failing (the result is not a "boolean" type). – Mark Wilkins Sep 22 '11 at 16:18
5

Please check the documentation of this function! It tells you the following:

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Try to check if the return value is nonzero, if it's not, try calling getlasterror and print that error message on the console and provide this information.

Thomas
  • 51
  • 1
  • The FileTimeToSystemTime() failes, and I get the error code 87 which means "The parameter is incorrect.". I have no idea what is wrong. *I have updated the code in the answer – Janman Sep 22 '11 at 16:11
  • I think Mark posted the solutions :) – Thomas Sep 23 '11 at 05:52
1

In the past, I have used WIN32_FILE_ATTRIBUTE_DATA instead of WIN32_FIND_DATA. Then to get the file's information, I use GetFileAttributesEx. An example is below:

string strFile = "c:\\myfile.txt";

WIN32_FILE_ATTRIBUTE_DATA    fileInfo;
// Get the attributes structure of the file
if ( GetFileAttributesEx(strFile, 0, &fileInfo) )
{
    SYSTEMTIME        stSystemTime;
    // Convert the last access time to SYSTEMTIME structure: 
    if ( FileTimeToSystemTime(&fileInfo.ftLastAccessTime, &stSystemTime) )
    {
        printf("Year = %d,  Month = %d,  Day = %d,  Hour = %d,  Minute = %d\n", stSystemTime.wYear, stSystemTime.wMonth, stSystemTime.wDay, stSystemTime.wHour, stSystemTime.wMinute);
    }
Eric R.
  • 1,105
  • 3
  • 21
  • 48
0

Shouldn't you use backslashes '\' in file path? Provided that this would correct your file path, the FindFirstFile API call might possibly succeed and would get you the time you needed.

Roman R.
  • 68,205
  • 6
  • 94
  • 158