CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2000
    Posts
    18

    Update file timestamp

    Is there a way to update the "last modified" attribute of a file without actually changing/copying the file contents?

    Is there some dll that can be linked in that has a unix-like "touch" command?


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Update file timestamp

    'The Win32 API includes native functions that
    'allow you To change a file's properties, such as time and date, without a
    'third-party DLL.

    Option Explicit
    '
    Private Type FILETIME
    dwLowDate As Long
    dwHighDate 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
    wMillisecs As Integer
    End Type

    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    Private Const GENERIC_WRITE = &H40000000
    Private Const OPEN_EXISTING = 3

    Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long
    Private Declare Function CreateFile Lib "kernel32" _
    Alias "CreateFileA" (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, ByVal dwShareMode _
    As Long, ByVal lpSecurityAttributes As Long, ByVal _
    dwCreationDisposition As Long, ByVal _
    dwFlagsAndAttributes As Long, ByVal hTemplateFile _
    As Long) As Long

    Private Declare Function LocalFileTimeToFileTime Lib _
    "kernel32" (lpLocalFileTime As FILETIME, lpFileTime _
    As FILETIME) As Long
    Private Declare Function SetFileTime Lib "kernel32" _
    (ByVal hFile As Long, ByVal MullP As Long, ByVal _
    NullP2 As Long, lpLastWriteTime As FILETIME) As Long
    Private Declare Function SystemTimeToFileTime Lib _
    "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime _
    As FILETIME) As Long

    Sub Main()

    Call SetFileDate("c:\autoexec.bat", "10/16/96 12:30 pm")

    End Sub


    Sub SetFileDate(sFileName As String, sDate As String)

    Dim hFile As Long
    Dim lResult As Long
    Dim udtSysTime As SYSTEMTIME
    Dim udtFileTime As FILETIME
    Dim udtLocalTime As FILETIME

    With udtSysTime
    .wYear = Year(sDate)
    .wMonth = Month(sDate)
    .wDay = Day(sDate)
    .wDayOfWeek = WeekDay(sDate) - 1
    .wHour = Hour(sDate)
    .wMinute = Minute(sDate)
    .wSecond = Second(sDate)
    End With

    lResult = SystemTimeToFileTime(udtSysTime, _
    udtLocalTime)
    lResult = LocalFileTimeToFileTime(udtLocalTime, _
    udtFileTime)

    hFile = CreateFile(sFileName, GENERIC_WRITE, _
    FILE_SHARE_READ _
    Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, _
    0, 0)

    lResult = SetFileTime(hFile, ByVal 0&, ByVal 0&, _
    udtFileTime)

    Call CloseHandle(hFile)

    End Sub



    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  3. #3
    Join Date
    Dec 2000
    Posts
    18

    Re: Update file timestamp

    Thank you!

    I will try your solution.


  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Update file timestamp

    rate it if it helped you

    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured