NTFS and last modification time on directory

Custom / CUSTOM
March 6, 2010 at 14:20:26
Specs: Microsoft Windows XP Professional, 2.401 GHz / 2047 MB
I'm using the "stat" function (stat.h) to determine the last modification time of a given file. I'm reading the "st_mtime" element to get that date correct. This all works OK, but the manual also states it cannot supply different info for Creation, Last Change and Access Time. Apparently, this is normal for FAT. Fair enough.

Now, I'm testing on NTFS, it still works ...

... but, when I query the information for a directory, whatever I do, he's giving me the creation time.

I'm running an old BorlandC (version 3.1). Do I just need a newer version, or is there an NTFS patch ?


See More: NTFS and last modification time on directory

Report •


#1
March 6, 2010 at 17:33:35
Edit:Changed "cin" to "cin.getline" for spaces...

I tried "stat" on dev-c++(where it's a legacy function) with
the same results as you mentioned. I'm not sure if it's
patchable or not but I wouldn't count on it.

What systems are you planning to use this on?

If your after portability you may have to find another library,
if it's just windows you may be able to access the api.


Here's a quick example I have knocked up in c++, according
to msdn they are all win2k and up functions(like so, so many are).

It would be easy to translate to c, but you probably will
need a newer compiler/libraries.


It will return file/folder time in gmt:

#include <windows.h>
#include <iostream>
using namespace std;


int main()
{
    HANDLE hfile;
    FILETIME cfile;
    SYSTEMTIME cftime;

    char inname[MAX_PATH];
    cout << "Gimme a file or directory name." << endl;
    cin.getline(inname,MAX_PATH);
    
    
    ZeroMemory(&cftime, sizeof(cftime));
    SetLastError(0);
    hfile = CreateFile(inname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    if (GetLastError())
        return 1;
    if (!GetFileTime(hfile, NULL, NULL, &cfile))
        return 1;
    if(!FileTimeToSystemTime(&cfile, &cftime))
        return 1;
    cout << cftime.wYear << " " << cftime.wMonth << " " << cftime.wDay << " " << cftime.wHour << ":" << cftime.wMinute;
    return 0;

}


EDIT:

Here is a local time version:

#include <windows.h>
#include <iostream>
using namespace std;


int main()
{
    HANDLE hfile;
    FILETIME stfile;
    FILETIME ltfile;
    SYSTEMTIME cftime;

    char inname[MAX_PATH];
    cout << "Gimme a file or directory name." << endl;
    cin.getline(inname,MAX_PATH);
    
    
    ZeroMemory(&cftime, sizeof(cftime));
    SetLastError(0);
    hfile = CreateFile(inname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    if (GetLastError())
        return 1;
    if (!GetFileTime(hfile, NULL, NULL, &stfile))
        return 1;
    if (!FileTimeToLocalFileTime(&stfile, <file))
        return 1;
    if(!FileTimeToSystemTime(<file, &cftime))
        return 1;
    cout << cftime.wYear << " " << cftime.wMonth << " " << cftime.wDay << " " << cftime.wHour << ":" << cftime.wMinute;
    return 0;
}


Report •

#2
Report •

#3
March 7, 2010 at 02:50:37
OK Thanks, I'll have a try with the above. The goal is indeed use on Windows (only), but it must work for files and directories, for both FAT32 and NTFS. I only need Last Modification Time ... it should return the same values which Explorer and DIR does (in case of both, using the default parameters, not forcing to show other times).

Report •

Related Solutions

#4
March 7, 2010 at 04:31:47
As far as I can tell the actual numbers are the same as "dir"(with the local time version), except you can also use seconds and milliseconds.

I have tested with fat32 and it seemed to work fine, though I do believe the limitations of fat are still very much present. Directories work too.


Report •

#5
March 14, 2010 at 05:57:14
I'm not doubting it works, but I don't have windows.h and winbase.h in my old compiler. Need to get a new one

Report •

#6
March 14, 2010 at 16:55:00
I'm not surprised borland c 3.1 is listed as 1992 in wiki, predating 32bit windows.

I used the default mingw compiler on Dev c++ 5 beta which in itself hasn't been updated since 2005.....

Any modern compiler should do the trick.


Report •

Ask Question