File Time Date Stamps

Page 2 of 3
  1. Introduction
  2. Setting Time Date Stamp
  3. Touch Module

Setting Time Date Stamp

One way to set the time date stamp on the local computer is to get the current time, set the clock's time to the time you want to stamp the file, open the file "As Binary", read the first byte and write it back, then close the file and reset the system clock to what the current time is supposed to be. For this to work, the user must have clock-setting privileges on their computer. This may not be the case. Also, you may run into problems with this when attempting to set a file date on a file that resides on the network.

The safe way to do this is using the WinAPI SetTime() function. I have written a simple BAS module that exposes the Touch() function and encapsulates its details. This is included in the Touch Module section. If all you want to do is set some times, then you can just download this source and plug it into your project and proceed. If you want to find out how it works, read on.

The Touch function takes two convenient parameters - the filename and the date-time you want to stamp it with. Touch willl return True if it worked and False if not. That way this can silently fail and it is the responsibility of your code to find out why, if you need to.

To use this function:

'example 1, string and date literals
Touch "C:SomeFile.txt", #11/12/2001 9:22 AM#

'example 2, variables
Touch sSomeFileName, dSomeDate

'example 3, touch with test
If Touch(sSomeFileName, dSomeDate) Then
    MsgBox "It worked"
Else
    MsgBox "It didn't work!"
End If

We open the file with the CreateFile() WinAPI function. The parameter dwDesiredAccess is set to GENERIC_WRITE, because we need to change the file. The parameter dwCreationDisposition is set to OPEN_EXISTING so that the function will silently fail if the file does not exist. CreateFile will return a valid Windows file handle if the function succeeded. We then convert the parameter dDate (the new timestamp) from a VB Date to a SYSTEMTIME structure using the standard VB date functions. Then we must convert this SYSTEMTIME to an 8-byte FILETIME value using SystemTimeToFileTime(). Then we get the current file's timestamp using GetFileTime, which returns three FILETIME values, one for CreationTime, one for LastAccessTime and one for LastWriteTime (these are the 3 dates you see in the File/Properties dialog in Windows Explorer). The last one, lpLastWriteTime, is the one that shows up in Windows Explorer and is returned by the VB FileDateTime function. We then must convert the new FILETIME, which is a function of the timezone of the computer, to a globally valid FILETIME, using the LocalFileTimeToFileTime() function. This makes sure that a file modified at 1:00 PM in New York will appear to have been modified at 10:00 AM in California. Then we replace the existing lpLastWriteTime with our new one, and call the SetFileTime() function to set the file's timestamp. If this works, it will return True (actually 1). All said and done, the last thing we must do is to close the file using the CloseHandle() function.

The source code for Touch() in TOUCH.BAS is included below.

Thankyou very much to Chris Velazquez for sharing this tip with us.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor