Library tutorials & articles
File Time Date Stamps
- Introduction
- Setting Time Date Stamp
- 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.
Related articles
Related discussion
-
Key_Press() event for text box
by Aquila (1 replies)
-
Regarding Visual Basic Programme
by manjunathsl2007 (0 replies)
-
how do you hide all in VB6
by CapnJack (1 replies)
-
Problem with Input File
by novavb6 (3 replies)
-
How to produce a txt file with a table??
by novavb6 (1 replies)
This is way overdue, but for anyone who IS looking at working out the date of a file:
Works with VB6 provided that the file already exists.
Won't create a new file, just a handle to an existing.
Regards
I agree! know i probably aware with this thing. I got something new to you! And I would like to share this with you. It's nice, I guarantee www.2-clicks-stamps.com. Please check this out!
Hi,
I thought this code was exactly what I wanted but it doesn't work with VB 2005. The first problem occurs in the CreateFile function with lpSecurityAttributes as "any" which is no longer used. I have no idea why this was called as Byval 0&. It's guaranteed not to work except in Win98 which ignores that parameter.
I defined lpSecurityAttributes as Integer (after trying almost everything else) then called it using 0 and that works! God knows why. But then the time function failed.
Anyway, I gave up. I'll write it in Delphi and then Shell it. It seems to me that VB 2005 has been designed to make it virtually inpossible to do API calls. Thank God for Borland.
Regards, Bob
This seems to be a very long-winded way of doing things.
I am looking for a simple code that will inform me of the date of any file.
I want to populate an excel sheet with the filedate of any file.
Is this possible without all this code ?
This thread is for discussions of File Time Date Stamps.