Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUpdate last modification and access times (touch)
Keywordsmodification, access, last modification time, last access time, touch, file, update, file times
CategoriesFiles and Directories
 
Sometimes it is convenient to modify a file's lasst access and modification time. For example, my system's clock was messed up for a while so I had several files with creation dates more than a year in the future. Also when people email me old files, their creation dates are long ago. When I list these files in Windows Explorer, I want to see them most-recent-on-top.

Unix systems have a touch command that sets a file's last modification time to the current time. That's what this example does.

This program uses Command$ to look through its command line parameters. For each file listed, the program uses the SetFileTime API function to update the file's last modification time. That counts as a file access so it automatically updates the file's last access time. See the code for a LOT of details.

 
' Set the modification time for the files listed
' in the programs command line parameters.
Private Sub Main()
Dim commands As String
Dim file_name As String
Dim pos As Integer
Dim time_now As Date

    time_now = Now

    ' Get the command line arguments.
    commands = Command$
    Do While Len(commands) > 0
        ' Get the next file's name.
        pos = InStr(commands, " ")
        If pos = 0 Then
            file_name = commands
            commands = ""
        Else
            file_name = Left$(commands, pos - 1)
            commands = Trim$(Mid$(commands, pos + 1))
        End If

        ' Update the file's last modification time. Note
        ' that
        ' setting the modification time counts as accessing
        ' the file so it also sets the last access time.
        If SetFileModifiedDate(file_name, time_now, True) _
            Then
            MsgBox "Error setting last modification time " & _
                "for " & _
                file_name
        End If
    Loop
End Sub

' Return True if there is an error.
Public Function SetFileModifiedDate(ByVal file_name As _
    String, ByVal modified_date As Date, ByVal local_times _
    As Boolean) As Boolean
Dim file_handle As Long
Dim modified_filetime As FILETIME
Dim file_time As FILETIME

    ' Assume something will fail.
    SetFileModifiedDate = True

    ' Convert the date into a FILETIME.
    modified_filetime = DateToFileTime(modified_date)

    ' Convert the file time into a system file time.
    If local_times Then
        LocalFileTimeToFileTime modified_filetime, file_time
        modified_filetime = file_time
    End If

    ' Open the file.
    file_handle = CreateFile(file_name, GENERIC_WRITE, _
        FILE_SHARE_READ Or FILE_SHARE_WRITE, _
        0&, OPEN_EXISTING, 0&, 0&)
    If file_handle = 0 Then Exit Function

    ' Set the time.
    If SetFileModifiedTime(file_handle, ByVal 0&, ByVal 0&, _
        modified_filetime) = 0 Then
        CloseHandle file_handle
        Exit Function
    End If

    ' Close the file.
    If CloseHandle(file_handle) = 0 Then Exit Function

    SetFileModifiedDate = False
End Function
 
You can use the program in at least three ways:
  • Drag and drop files onto the program's executable
  • Run it at a DOS prompt, as in touch.exe file1 file2 file3
  • Install the program in Windows Explorer's Send To menu and send files to it
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated