Results 1 to 5 of 5

Thread: Changing the date of a file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 1999
    Location
    Germany
    Posts
    17
    Hi,

    I try to change the date of a file:

    Code:
    MsgBox FileSystem.FileDateTime("C:\temp\test.txt")
    No problem! I can see the date of the file!

    Code:
    FileSystem.FileDateTime("C:\temp\test.txt") = Now - 3
    But why doesn't that work! VB says "Object Required"

    Thanks for your help!
    Frank
    VB-progress: -> -> ->

  2. #2
    Member
    Join Date
    Jun 2000
    Location
    North of France
    Posts
    49

    Arrow

    At the moment, i can tell you why it doesn't work.

    Because FileDateTime allow you to get the date, not to set the date. It's a kind of function that return a value.
    Hi-Tech Engineer

  3. #3
    Member
    Join Date
    Jun 2000
    Location
    North of France
    Posts
    49

    Arrow

    To change the date, try an API. Link:
    http://www.vbapi.com/ref/s/systemtimetofiletime.html

    Hope that'll help
    Hi-Tech Engineer

  4. #4
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845



    Code:
    Option Explicit
     
    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
    
    Private Const GENERIC_WRITE = &H40000000
    Private Const OPEN_EXISTING = 3
    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    
    Private Declare Function SetFileTimeWrite Lib "kernel32" Alias "SetFileTime" (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
    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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
    
    Private Sub Command1_Click()
        Dim year As Integer, Month As Integer
        Dim Day As Integer, Hour As Integer
        Dim Minute As Integer, Second As Integer
        Dim TimeStamp As Variant
        Dim Filename As String
        Dim X As Integer
        Dim i As Integer
        
        
          year = Format(Date, "yyyy")
          Month = Format(Date, "mm")
          Day = Format(Date, "dd")
          Hour = Format(Time, "hh")
          Minute = Format(Time, "nn")
          Second = Format(Time, "ss")
      
        
        TimeStamp = DateSerial(year, Month, Day) + TimeSerial(Hour, Minute, Second)
      
          X = ModifyFileStamp(Filename, TimeStamp)
     
    End Sub
    
    Function ModifyFileStamp(Filename As String, TimeStamp As Variant) As Integer
        Dim X As Long
        Dim Handle As Long
        Dim System_Time As SYSTEMTIME
        Dim File_Time As FILETIME
        Dim Local_Time As FILETIME
    
        System_Time.wYear = year(TimeStamp)
        System_Time.wMonth = Month(TimeStamp)
        System_Time.wDay = Day(TimeStamp)
        System_Time.wDayOfWeek = Weekday(TimeStamp) - 1
        System_Time.wHour = Hour(TimeStamp)
        System_Time.wMinute = Minute(TimeStamp)
        System_Time.wSecond = Second(TimeStamp)
        System_Time.wMilliseconds = 0
    
        'convert the system time to a file time
        X = SystemTimeToFileTime(System_Time, Local_Time)
    
        'convert local file time to file time based on UTC
        X = LocalFileTimeToFileTime(Local_Time, File_Time)
    
        'open the file so we can get a file handle to the file
        Handle = CreateFile(Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    
        'now change the file time and date stamp
        X = SetFileTimeWrite(Handle, ByVal 0&, ByVal 0&, File_Time)
        CloseHandle Handle
    
    End Function
    Mark
    -------------------

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 1999
    Location
    Germany
    Posts
    17

    Thumbs up THANK U all

    Thanks MARK.... WORKS PERFECT!!!!
    Frank
    VB-progress: -> -> ->

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