How to Retrieve a File's Time/Date Stamp in Visual Basic

ID Number: Q80644

1.00

WINDOWS

Summary:

Visual Basic does not include a function that will retrieve a file's

date and/or time "stamp." To retrieve a file's time/date stamp, a

Visual Basic application can Shell to MS-DOS and redirect a directory

listing into a temporary text file. The application can then read the

contents of this temporary file and parse out the file's time/date

stamp. This technique is illustrated in the following example.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

This example uses the Windows API functions GetWindow and IsWindow

to determine when the task created by the Shell is finished.

For more information on GetWindow and IsWindow, query on the following

words:

GetWindow and IsWindow

To retrieve a file's time/date stamp, do the following:

1. Start Visual Basic, or from the File menu, choose New Project

(ALT, F, N) if Visual Basic is already running. Form1 will be

created by default.

2. Create the following controls for Form1.

Control CtlName Property Setting

------- ------- ----------------

Drive list box Drive1

Directory list box Dir1

File list box File1

Label Label1 Caption = ""

Label Label2 Caption = ""

Label Label3 Caption = ""

3. Add the following code to the general Declarations section of

Form1:

Declare Function GetActiveWindow% Lib "user" ()

Declare Function IsWindow% Lib "user" (ByVal hWnd%)

4. Add the following code to the Drive1_Change event procedure of

Form1:

Sub Drive1_Change ()

Dir1.Path = Drive1.Drive

End Sub

5. Add the following code to the Dir1_Change event procedure of Form1:

Sub Dir1_Change ()

File1.Path = Dir1.Path

End Sub

6. Add the following code to the File1_Change event procedure of

Form1:

Sub File1_Click ()

'Disable file control to prevent recursive DoEvents()

File1.Enabled = 0

'Redirect output of DIR to vbtmp.dat.

Path$ = Dir1.Path + "\"

FileSpec$ = File1.List(File1.ListIndex)

Tmp$ = "Dir " + Path$ + FileSpec$

Tmp$ = Tmp$ + "> vbtmp.dat"

x% = Shell ("Command.com /c " + Tmp$, 1)

x% = DoEvents()

' Loop until task created by Shell is done.

ShellhWnd% = GetActiveWindow()

While IsWindow% (ShellhWnd%)

x%=DoEvents()

Wend

' Read DIR info from vbtmp.dat.

Open Path$ + "vbtmp.dat" For Input As #1

For i%=1 To 4

Input #1, Dummy$

Next i%

Input #1, Inp$

Close #1

Kill Path$ + "vbtmp.dat" ' Delete vbtmp.dat

Label1.Caption = "Date: " + Mid$(Inp$, 24, 8)

Label2.Caption = "Time: " + LTrim$(Mid$(Inp$, 34, 6))

Label3.Caption = "Size: " + LTrim$(Mid$(Inp$, 13, 9))

File1.Enabled = -1

End Sub

7. Run the program. Clicking on any file listed in the file list box

will display the selected file's time/date stamp and file size in

the appropriate label.

Reference(s):

"Programming Windows: the Microsoft Guide to Writing Applications for

Windows 3," Charles Petzold, Microsoft Press, 1990

"Microsoft Windows Software Development Kit Reference Volume 1,"

version 3.0.

WINSDK.HLP file shipped with Microsoft Windows 3.0 Software

Development Kit

Additional reference words: 1.00