CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 1999
    Location
    Sacramento
    Posts
    9

    How to set file date?

    I am dealing with a minor Y2K bug, and need to be able to automate setting a number of files' time/date stamp to a specific date (in 1999!) to test a workaround. Does anybody know an easy way to do this? Perhaps a call to an SDK function?

    Dave Straayer

    Dave Straayer
    Varatouch, Inc.

  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: How to set file date?

    You can do this with the WinAPI SetFileTime call - here's an example - create a new project with a form (FORM1) and a button (Command1), then paste in the following code :


    option Explicit
    '
    '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.
    '
    '
    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
    '
    private 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
    '
    private Sub Command1_Click()
    '
    ' Of course, this only works if you have the relevant file
    ' on your harddisk (ie. it exists!)
    '
    SetFileDate "c:\chris.dat", "01/01/2000"
    End Sub





    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

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