Home > Windows API > touch!!


Remember good old unix/linux command called touch, so simple but very useful, it changes the modified and last access time of a file. Ever felt the need for the same in windows, well I did sometime back.

There is a function in imagehlp.dll which is called TouchFileTimes. This is how we use it…

// Include file
#include "Imagehlp.h"    

// Link to this library
#pragma comment( lib, "ImageHlp.lib" )    

int main( int argc, char **argv )
    HANDLE hFile = CreateFile( "C://SomeFile.ext", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
    ASSERT( hFile );

    // If second parameter is NULL then system time will be used, or the given date time
    TouchFileTimes( hFile, 0 );
    CloseHandle( hFile ); 

    return 0;
}// End main


Right click and open properties for the file and see what’s the last modified time,
it will be the current system time.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: