Help with creating function, GetLastTimeFileModified

Can someone help me solve my problem. I need to create a function that checks a file and returns the date that the file has last been modified.

I've found this: https://docs.microsoft.com/en-us/windows/desktop/SysInfo/retrieving-the-last-write-time

however I don't understand windows side of things.
How can I get a handle to an already created file?

Thanks
The example you link to is a complete program that will open a pre-existing file whose name is given on the command line. Don't be fooled by the function name "CreateFile", it also opens pre-existing files. That's what the OPEN_EXISTING flag is for.
I'm trying to find a way using the windows API to open an already created file and grab the last time its been modified and save that information.

I'm looking into learning win32 API now to better understand how windows does things.

**Does anyone know where I can find an simple program created with win32 API that is a good template / modularized.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <windows.h>

// return utc system time as a string in international standard format
std::string to_string( SYSTEMTIME time )
{
    std::ostringstream stm ;
    stm << time.wYear << '-' << std::setfill('0')
        << std::setw(2) << time.wMonth << '-'
        << std::setw(2) << time.wDay << 'T'
        << std::setw(2) << time.wHour << ':'
        << std::setw(2) << time.wMinute << ':'
        << std::setw(2) << time.wSecond << '.'
        << std::setw(3) << time.wMilliseconds << "Z" ;

    return stm.str() ;
}

std::string last_write_time( std::string path_to_file )
{
    // try to open an existing file for generic read access
    HANDLE file = ::CreateFileA( path_to_file.c_str(), GENERIC_READ, FILE_SHARE_READ,
                                 nullptr, OPEN_EXISTING, 0, nullptr ) ;

    // attempt to open the file failed: typically, file does not xist or access is denied
    if( file == INVALID_HANDLE_VALUE ) return "error: failed to open file" ;

    // retrieve the last write time of the file
    ::FILETIME last_write_time ;
    if( !::GetFileTime( file, nullptr, std::addressof(last_write_time), nullptr ) )
        return "error: failed to retrieve last write time" ;

    // convert the last write time of the file to UTC time
    SYSTEMTIME utc_time ;
    ::FileTimeToSystemTime( std::addressof(last_write_time), std::addressof(utc_time) ) ;

    // return the time formatted as a string
    return to_string(utc_time) ;
}

int main()
{
    const char* const path_to_file = __FILE__ ; // this file

    std::cout << "file: " << path_to_file
              << "\nlast write time: " << last_write_time(path_to_file) << '\n';
}

https://rextester.com/GVJE7926
Awesome Thanks!

How do I convert it to my System time and not utc?



> How do I convert it to my System time and not utc?

SystemTimeToTzSpecificLocalTime()
https://docs.microsoft.com/en-us/windows/desktop/api/timezoneapi/nf-timezoneapi-systemtimetotzspecificlocaltime

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <windows.h>

// return system time as a string
std::string to_string( SYSTEMTIME time )
{
    std::ostringstream stm ;
    stm << time.wYear << '-' << std::setfill('0')
        << std::setw(2) << time.wMonth << '-'
        << std::setw(2) << time.wDay << ' '
        << std::setw(2) << time.wHour << ':'
        << std::setw(2) << time.wMinute << ':'
        << std::setw(2) << time.wSecond << '.'
        << std::setw(3) << time.wMilliseconds ;

    return stm.str() ;
}

std::string local_time( FILETIME ftime )
{
    SYSTEMTIME utc ;
    ::FileTimeToSystemTime( std::addressof(ftime), std::addressof(utc) ) ;

    SYSTEMTIME local ;
    ::SystemTimeToTzSpecificLocalTime( nullptr, std::addressof(utc), std::addressof(local) );
    
    return to_string(local) ;
}

std::string last_write_time( std::string path_to_file )
{
    // try to open an existing file for generic read access
    HANDLE file = ::CreateFileA( path_to_file.c_str(), GENERIC_READ, FILE_SHARE_READ,
                                 nullptr, OPEN_EXISTING, 0, nullptr ) ;

    // attempt to open the file failed: typically, file does not xist or access is denied
    if( file == INVALID_HANDLE_VALUE ) return "error: failed to open file" ;

    // retrieve the last write time of the file
    ::FILETIME last_write_time ;
    if( !::GetFileTime( file, nullptr, std::addressof(last_write_time), nullptr ) )
        return "error: failed to retrieve last write time" ;

    // return the local time formatted as a string
    return local_time(last_write_time) ;
}

int main()
{
    const char* const path_to_file = __FILE__ ; // this file

    std::cout << "file: " << path_to_file
              << "\nlast write time (local time): " << last_write_time(path_to_file) << '\n';
}

https://rextester.com/WSOM75693
Topic archived. No new replies allowed.