Modifying the Last Modified Date and Time 
Author Message
 Modifying the Last Modified Date and Time

Hi there,

Could somebody tell me if it is possible to change the last modified
property of a file.

Also if it is very similar, how to change the other properties on that
screen.

Thanks in advance

Please post to the newsgroup so that all can feast upon your genius

Steven Knight



Sat, 19 Apr 2003 02:29:39 GMT  
 Modifying the Last Modified Date and Time
Hi Steven,

Here is some sample code that will do what you want:

1. Open a new project
2. Place a CommonDialog1 control on Form1.
3. Place a Command1 button on Form1
4. Paste the following code onto the Form1:

Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Boolean
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

Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const CREATE_ALWAYS = 2
Private Const CREATE_NEW = 1
Private Const OPEN_ALWAYS = 4
Private Const TRUNCATE_EXISTING = 5

Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_COMPRESSED = &H800
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
Private Const INVALID_HANDLE_VALUE = -1&

Private Declare Function SystemTimeToFileTime Lib _
        "kernel32" (lpSystemTime As SYSTEMTIME, _
                    lpFileTime As FILETIME) As Long
Private Declare Function SetFileTime Lib _
        "kernel32" (ByVal hFile As Long, _
                    lpCreationTime As Any, _
                    lpLastAccessTime As Any, _
                    lpLastWriteTime As FILETIME) As Long
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, _
         lpSecurityAttributes As Any, _
         ByVal dwCreationDisposition As Long, _
         ByVal dwFlagsAndAttributes As Long, _
         ByVal hTemplateFile As Long) As Long
Private Declare Function FileTimeToSystemTime Lib _
        "kernel32" (lpFileTime As FILETIME, _
                    lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib _
        "kernel32" (lpFileTime As FILETIME, _
                    lpLocalFileTime As FILETIME) 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 LocalFileTimeToFileTime Lib _
        "kernel32" (lpLocalFileTime As FILETIME, _
                    lpFileTime As FILETIME) As Long

'Format Constants
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const FORMAT_MESSAGE_FROM_STRING = &H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF

Public Function SetMyTime(sFname As String, sDate As String, sTime As
String) As Boolean
    'sFname - file name
    'sDate - the Date in format "yyyy/mm/dd" or "mm/dd/yyyy" or "dd/mm/yyyy"
    'or any other valid date format
    'sTime - the Time in format "hh:mm:ss"
    ''''''''''''''''''''''''''''''''''''''''''''''''''''
    'On success returns TRUE; On failure returns FALSE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim SA As SECURITY_ATTRIBUTES
    Dim ST As SYSTEMTIME
    Dim hFile As Long
    Dim FT As FILETIME
    Dim lRetVal As Long

    On Error GoTo SetFileTimeError

    SetMyTime = False ' Reset the function

    'Fill up the ST structure
    ST.wYear = Year(sDate)
    ST.wMonth = Month(sDate)
    ST.wDayOfWeek = Weekday(sDate)
    ST.wDay = Day(sDate)
    ST.wHour = Hour(sTime)
    ST.wMinute = Minute(sTime)
    ST.wSecond = Second(sTime)
    ST.wMilliseconds = 0

    'Fill up the SA structure
    SA.nLength = Len(SA)
    SA.lpSecurityDescriptor = 0
    SA.bInheritHandle = 0

    'Open an existing file
    hFile = CreateFile(sFname, GENERIC_WRITE, 0, _
               ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)

    If hFile <> INVALID_HANDLE_VALUE Then
        'Convert the time to FILETIME
        lRetVal = SystemTimeToFileTime(ST, FT)

        lRetVal = LocalFileTimeToFileTime(FT, FT)

        If lRetVal <> 0 Then
            'Set the Creation time, Last Access time and Last Write time to
            'the same value.
            'You could leave any of them unmodified by passing a 0& instead
            ' For example, here we modify only the Last Write time
            '   lRetVal = SetFileTime(hFile, &0, &0, FT)

            'Modify all 3 of the file time atributes.
            lRetVal = SetFileTime(hFile, FT, FT, FT)

            If lRetVal <> 0 Then
                'Close the file
                CloseHandle hFile
                SetMyTime = True
              Else
                 'Get the DLL error caused by the API
                 'translate it to VB error.
                 Err.Raise Err.LastDllError
            End If
          Else
            'Close the file
            CloseHandle hFile
        End If
      Else
        'Get the DLL error caused by the API
        'translate it to VB error.
        Err.Raise Err.LastDllError
    End If

    Exit Function

SetFileTimeError:
    'Close the file
    CloseHandle hFile

    MsgBox "Error " & CStr(Err.Number) & " " & Err.Description

    Err.Clear
End Function

Private Sub Command1_Click()
        Dim sFname As String
        Dim sDate As String
        Dim sTime As String

        CommonDialog1.ShowOpen

        sFname = CommonDialog1.FileName

        sDate = InputBox("Enter new date (dd/mm/yyyy):")
        sTime = InputBox("Enter new time (hh:mm:ss):")
    If Trim$(sDate) <> "" And Trim$(sTime) <> "" Then
        If SetMyTime(sFname, sDate, sTime) Then
            MsgBox "Success"
          Else
            MsgBox "Failure"
        End If
    End If
End Sub

5. Run the project and press the Command1 button. Then sellect a file from
the OpenFile dialog and when  prompted, enter a date and time to set as this
file time/date attributes.

NOTE also, that under NT you may need  proper access rights to modify some
files' attributes. Also, under NT I have found, that if you try to modify
only, let's say the Last Write attribute, in such way, that it will
contradict the Creation Time, SetFileTime() will fail.

Regards,
              Stoil


Quote:
> Hi there,

> Could somebody tell me if it is possible to change the last modified
> property of a file.

> Also if it is very similar, how to change the other properties on that
> screen.

> Thanks in advance

> Please post to the newsgroup so that all can feast upon your genius

> Steven Knight



Sat, 19 Apr 2003 04:39:47 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Read file Date/Time last modified

2. Reading last modified date time

3. Change the Last Modify and Last Access date

4. Modules and date last modified

5. How to look for Last Modified Date...

6. List Access Objects based on Date Last Modified

7. Date Last Modified

8. Word: Filter files on date last modified

9. Check the last modified date on any file

10. Access Last Date Modified

11. Determine file last modified date.

12. Need to display last modified date for files in web page

 

 
Powered by phpBB® Forum Software