* VB-CODE (1)
Tip 127: Stamping Files with the Current Date and Time
July 1, 1995
While your Microsoft=AE Visual Basic=AE program is running, you may need =
to update the
date and time stamp for a particular file. This article explains how you =
can set a
file's date and time stamp to the current date and time setting.
Manipulating a File's Date and Time Information
The FileDateTime function in Microsoft=AE Visual Basic=AE can tell you =
when a specific
file was first created or last modified. FileDateTime returns both the =
time information as a string variable. However, Visual Basic does not =
function to set a file's date and time information.
The example program below shows how to update the date and time =
information for an
already existing file. First, you need to make sure the file exists. The =
function will return a NULL or empty string if it cannot find the =
on the disk.
When you know the file exists, you can use the Visual Basic file =
commandsOpen, Get, Put, and Closeto force the operating system to update =
file's date and time stamp.
The technique is straightforward. You simply open the file, use the Get =
to retrieve the first byte from the file, and then use the Put statement =
that same byte back to the file. When you close the file, Microsoft =
automatically updates the file's date and time information.
This program shows how to update a file's date and time information from =
Visual Basic application.
1. Create a new project in Visual Basic. Form1 is created by default.
2. Add a Command Button control to Form1. Command1 is created by =
3. Add the following code to the Click event for Command1:
Private Sub Command1_Click()
Dim FName As String
Dim F As String
Dim AnyThing As Integer
Dim X As Integer
FName =3D "c:\test.doc"
F =3D Dir$(FName)
If F =3D "" Then GoTo NoSuchFile
On Error GoTo FileError
X =3D FreeFile
Open FName For Binary As X
Get X, 1, AnyThing
Put X, 1, AnyThing
MsgBox "New time/date is: " & FileDateTime("c:\test.doc"), 16, "OK"
MsgBox "Unable to time-stamp file", 16, "Error"
MsgBox "That file does not exist!", 16, "Error"
Note This example program assumes you have a file named TEST.DOC in =
directory of drive C.
Run the example program by pressing F5. Click the command button to =
file's date and time information.
"FileDateTime Function." (Development Library, Product Documentation, =
Visual Basic 4.0 Professional and Enterprise Editions, Language =
Knowledge Base Q113958. "BUG: VBApp FileCopy Updates Destination File's =
Date & Time Stamp."
Knowledge Base Q96098. "FileDateTime Doesn't Include Time If File Time =