Library tutorials & articles

File Time Date Stamps

Touch Module

'Touch.bas - VB code to set a file's date and time safely
'
'        Courtesy of VDEV.NET - developers of Windows and Internet software
'        Want to find out more?  mailto:chris@vdev.net
'
'


Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Private Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
End Type

'Made private to prevent namespace pollution

Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const OPEN_EXISTING = 3
Private Const INVALID_HANDLE_VALUE = -1

Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, _
    ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Public Function Touch(ByVal sFileName As String, ByVal dDate As Date) As Boolean
Dim hFile As Long
Dim iResult As Long

Dim lpCreationTime As FILETIME
Dim lpLastAccessTime As FILETIME
Dim lpLastWriteTime As FILETIME
Dim lpLocalFileTime As FILETIME
Dim lpSystemTime As SYSTEMTIME

    hFile = CreateFile(sFileName, GENERIC_WRITE, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
    If hFile <> INVALID_HANDLE_VALUE Then
        With lpSystemTime
            .wDay = Day(dDate)
            .wMonth = Month(dDate)
            .wYear = Year(dDate)
            .wHour = Hour(dDate)
            .wMinute = Minute(dDate)
            .wSecond = Second(dDate)
        End With
        iResult = SystemTimeToFileTime(lpSystemTime, lpLocalFileTime)
        If iResult Then
            iResult = GetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime)
            If iResult Then
                iResult = LocalFileTimeToFileTime(lpLocalFileTime, lpLastWriteTime)
                If iResult Then
                    iResult = SetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime)
                    Touch = CBool(iResult)
                Else
                    Touch = False
                End If
            Else
               Touch = False
            End If
        Else
            Touch = False
        End If
        CloseHandle hFile
    Else
       Touch = False
    End If
End Function

Comments

  1. 09 Nov 2008 at 23:37

    This is way overdue, but for anyone who IS looking at working out the date of a file:

    My.Computer.FileSystem.GetFileInfo("filename.ext").LastWriteTime.ToShortDateString
    
  2. 06 Aug 2008 at 20:20

    Works with VB6 provided that the file already exists.

    Won't create a new file, just a handle to an existing.

     Regards

     

  3. 10 Oct 2007 at 04:45

     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!

  4. 22 Mar 2007 at 06:35

    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

     

     

  5. 07 May 2006 at 11:39

    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 ?

     

  6. 01 Jan 1999 at 00:00

    This thread is for discussions of File Time Date Stamps.

Leave a comment

Sign in or Join us (it's free).