🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2124

Re: How to effectively modify the last access time of a file

#2124
From: Darko Miletic
Date: Sun, 08 Apr 2007 00:15
68 lines
3273 bytes
JF Jolin wrote:
> Here is the scenario:
> 
> hFile = CreateFile( lpszFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
> 
> SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );
> 
> CloseHandle( hFile );
> 
> 
> Whatever time is specified in fTimeLastAccess, that will be overwritten when CloseHandle instruction will be executed.

MSDN that states : "lpLastAccessTime
[in] A pointer to a FILETIME structure that contains the new date and 
time the file was last accessed. The last access time includes the last 
time the file was written to, read from, or (in the case of executable 
files) run. This parameter can be NULL if the application does not need 
to set this information."
And now the crucial line:
"To preserve the existing last access time for a file even after 
accessing a file, call SetFileTime with this parameter set to -1 before 
closing the file handle."

Here is the working sample:

#include <windows.h>
#include <string>
#include <iostream>
#include <ctime>

//Important kb about converting time
//http://support.microsoft.com/kb/167296

void UnixTimeToFileTime(std::time_t t, LPFILETIME pft)
{
  LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000i64;
  pft->dwLowDateTime = (DWORD)ll;
  pft->dwHighDateTime = ll >> 32;
}

void changeFileTime(const std::string &fname) {

  HANDLE hFile = CreateFile( fname.c_str(),
                             GENERIC_READ | GENERIC_WRITE,
                             0,
                             NULL,
                             OPEN_EXISTING,
                             FILE_ATTRIBUTE_NORMAL,
                             NULL );
  if (hFile != INVALID_HANDLE_VALUE){
    FILETIME ft = {0UL}, ftminus = {-1L,-1L};
    std::tm tmp = {0,0,14,7,3,2007 - 1900,0,0,0};
    std::time_t timeValue = std::mktime(&tmp);
    UnixTimeToFileTime(timeValue,&ft);
    BOOL ret = SetFileTime(hFile,NULL,&ft,NULL);
    ret = ret && SetFileTime(hFile, NULL, &ftminus, NULL);
    if (ret == FALSE) {
      std::cout << "could not change last access field." << std::endl;
    }
    CloseHandle(hFile);
  }

}

int main () {
	changeFileTime("<some existing file here>");
   return 0;
}
Technical Details
Message-ID: <46183403@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <op.tqajwtlyjr09w3@localhost>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,69dd2e4db7d54b87
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Sat, 07 Apr 2007 21:15:30 -0300
From: Darko Miletic <kiklop@fibertel.com.ar>
User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: How to effectively modify the last access time of a file
References: <op.tqajwtlyjr09w3@localhost>
In-Reply-To: <op.tqajwtlyjr09w3@localhost>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 000731-0, 06/04/2007), Outbound message
X-Antivirus-Status: Clean
NNTP-Posting-Host: 200.89.185.153
Message-ID: <46183403@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1175991299 200.89.185.153 (7 Apr 2007 17:14:59 -0800)
Lines: 68
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail