LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I determine when a file was last modified using CVI?

I want to have my CVI program check a file that is written from a seperate program to determine if it is being updated. How can I check the modified attribute of this file?
0 Kudos
Message 1 of 8
(5,102 Views)
Found it in the utility library (GetFileTime())
0 Kudos
Message 2 of 8
(5,098 Views)
I'm having the same problem right now.

GetFileTime()/GetFileDate() appear to return the file creation time, not the last modification time.

I am fervently hoping there is a way to get the true modification time, without resorting to the Windows SDK GetFileTime() function. The thought of coding up 64-bit arithmetic in CVI is too horrible to contemplate.
0 Kudos
Message 3 of 8
(5,080 Views)
Using Windows 2000 and CVI 6.0, I see CVI's GetFileTime (in utility.h) returning the Modified time, not the Created time. I tried multiple files, including files I created from scratch and files I copied from existing files. Every time I save the file, GetFileTime sees the new time.
0 Kudos
Message 4 of 8
(5,077 Views)
Yeah, the GetFileTime seems to working alright. Here is a code snippit...

GetFileTime(myFile, &hr, &min, &sec);

if(hr == lastHr && min == lastMin && sec == lastSec)
{ // the file is updating correctly
}
else
{ // it's not
}
lastHr = hr;
lastMin = min;
lastSec = sec;

Message Edited by sBagg on 04-14-2005 12:30 PM

0 Kudos
Message 5 of 8
(5,076 Views)
Yeah, it was actually working. Turns out the problem was my conversion to a time_t was failing, because I forgot to subtract 1900 from the year. I always got the same value (the unsigned interpretation of -1), so I thought GetFileTime was always returning the same result.

Anyway, since I already wrote the code to use the Windows GetFileTime() API, here it is. The ugly 64-bit arithmetic turned out to be unnecessary. It can be fairly easily modified to get creation or last-access times (which CVI's GetFileTime() will not do.)

Now I just need to sort out why my time_t results are 70 years higher than TestStand's Seconds(False) function. I suspect TestStand is using mktime() internally, and incorrectly subtracting 1970 from the year, instead of 1900.

-------------

#include

int DLLEXPORT __stdcall GetFileTimestamp( const char *filename, time_t *timestamp )
{
int error = 0;
HANDLE f = INVALID_HANDLE_VALUE;
FILETIME ftWrite;
SYSTEMTIME stUTC;
struct tm t;
time_t tt;

f = CreateFile( filename, GENERIC_READ, 0/*dwShareMode*/, NULL/*lpSecurityAttributes*/,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0/*hTemplateFile*/ );
if( f == INVALID_HANDLE_VALUE ) {
errChk( -1 );
}

// Get FILETIME, 100ns intervals since 1601 (UTC time)
#undef GetFileTime // Don't want the CVI version.
if( !GetFileTime( f, NULL/*lpCreationTime*/, NULL/*lpLastAccessTime*/, &ftWrite ) ) {
errChk( -2 );
}
// Convert to SYSTEMTIME, Y/M/D/h/m/s structure (still UTC)
if( !FileTimeToSystemTime(&ftWrite, &stUTC) ) {
errChk( -3 );
}
// Convert to Unix time_t (seconds since 1970, still UTC)
memset( &t, 0, sizeof(t) );
t.tm_year = stUTC.wYear-1900;
t.tm_mon = stUTC.wMonth-1; // struct tm uses 0-based month for some reason.
t.tm_mday = stUTC.wDay;
t.tm_hour = stUTC.wHour;
t.tm_min = stUTC.wMinute;
t.tm_sec = stUTC.wSecond;
t.tm_isdst = 0; // UTC has no daylight savings time, I believe.
tt = mktime(&t);

*timestamp = tt;

Error:
if( f != INVALID_HANDLE_VALUE )
CloseHandle(f);
return( error );
}
0 Kudos
Message 6 of 8
(5,073 Views)
There appears to be a bug in the LabWindows/CVI 6.0 implementation of mktime(), which I mentioned here: http://forums.ni.com/ni/board/message?board.id=180&message.id=14586

To get around it,
t.tm_year = stUTC.wYear-1900
must be changed to
t.tm_year = stUTC.wYear-1970

I hope that's the only bug.

- Ron
0 Kudos
Message 7 of 8
(5,064 Views)
Ok, it turns out I was wrong about bugs in mktime() and time(). CVI defines time_t as seconds since 1900, not seconds since 1970 like Unix, TestStand and Visual C++ do. It is a great disappointment to me that ANSI standard leaves something so important implementation-defined.

Anyway, there's also a bug in the code I gave above. mktime() requires local time, I'm providing it UTC time. So, that code will not work as it stands.
0 Kudos
Message 8 of 8
(5,051 Views)