INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

API Functions

Change a File's Date and Time? (ie. "Touch") by wgcs
Posted: 6 Aug 03 (Edited 6 Aug 03)

This used to be easy: Just use TOUCH.COM ... however, while Touch still works, in Windows it brings up an ugly black Command Prompt window while it executes. Plus, who knows if you have rights to redistribute TOUCH.COM?!?

Anyway, Here is a way to Programmatically TOUCH from within VFP.  This code Requires the "Struct" class (available here: http://fox.wikis.com/wc.dll?Wiki~ApiStructureClass )  to be available to handle the API structures.

( I use a crude way of getting the TimeZone Bias here (all file date/times are stored in UTC)... you could use the method in FAQ184-3322 to do so... so far, I've been too lazy to integrate it... )

PROCEDURE Touch( pcFileName, ptDateTime )
  * This is based on VB code found here:
  *   http://www.blackbeltvb.com/free/setftime.htm
  #DEFINE OF_WRITE            0x0001
  #DEFINE OFS_MAXPATHNAME     128

  Declare LONG CloseHandle IN "kernel32" LONG hObject
  Declare LONG GetFileTime IN "kernel32" LONG hFile, ;
    STRING @ FILETIME_lpCreationTime,   ;
    STRING @ FILETIME_lpLastAccessTime, ;
    STRING @ FILETIME_lpLastWriteTime
  Declare LONG OpenFile    IN "kernel32" ;
    STRING @ lpFileName, ;
    STRING @ OFSTRUCT_lpReOpenBuff, ;
    LONG   wStyle
  *' Note I've changed the lpLastAccessTime from a FILETIME to a LONG, because I'm going to pass a 0 (Null)
  *'cause I don't want to set it.
  Declare LONG SetFileTime IN "kernel32" LONG hFile, ;
    STRING @ FILETIME_lpCreationTime, ;
    LONG   lpLastAccessTime, ;
    STRING @ FILETIME_lpLastWriteTime
  Declare LONG FileTimeToSystemTime IN "kernel32" ;
    STRING @ FILETIME_lpFileTime, ;
    STRING @ SYSTEMTIME_lpSystemTime
  Declare LONG SystemTimeToFileTime IN "kernel32" ;
    STRING @ SYSTEMTIME_lpSystemTime, ;
    STRING @ FILETIME_lpFileTime

  LOCAL hFile, OFS, lnL, cTime, lTime, lwTime, sTime
  LOCAL lcOFS, lcFileName, lc_cTime, lc_lTime, lc_lwTime
  
  OFS = CREATEOBJECT('OFSTRUCT')
  lcOfs = OFS.Structure
  lcFileName = alltrim(pcFileName)+chr(0)
  hFile = OpenFile(@lcFilename, @lcOFS, OF_WRITE)
  If hFile <= 0
    RETURN .F.
  ELSE
    cTime  = CREATEOBJECT('FILETIME')
    lTime  = CREATEOBJECT('FILETIME')
    lwTime = CREATEOBJECT('FILETIME')
    lc_cTime  = cTime.Structure
    lc_lTime  = lTime.Structure
    lc_lwTime = lwTime.Structure
    lnL=GetFileTime(hFile, lc_cTime, lc_lTime, lc_lwTime)
    cTime.Structure  = lc_cTime
    lTime.Structure  = lc_lTime
    lwTime.Structure = lc_lwTime
    
    sTime = CREATEOBJECT('SYSTEMTIME')
    
    lc_sTime = sTime.Structure
DECLARE GetLocalTime  IN Win32API STRING @ LPSYSTEMTIME_lpSystemTime
GetLocalTime(@lc_sTime)
sTime.Structure = lc_sTime
ltLocalTime = DateTime( sTime.fld('wYear'), sTime.fld('wMonth'), sTime.fld('wDay'), ;
                        sTime.fld('wHour'), sTime.fld('wMinute'), sTime.fld('wSecond') )

*!*    ?'Year=',sTime.fld('wYear')  
*!*    ?'Month=',sTime.fld('wMonth')
*!*    ?'Day=',sTime.fld('wDay')    
*!*    ?'Hour=',sTime.fld('wHour')  
*!*    ?'Min=',sTime.fld('wMinute')
*!*    ?'Sec=',sTime.fld('wSecond')
DECLARE GetSystemTime IN Win32API STRING @ LPSYSTEMTIME_lpSystemTime
GetSystemTime(@lc_sTime)
sTime.Structure = lc_sTime
ltSysTime = DateTime( sTime.fld('wYear'), sTime.fld('wMonth'), sTime.fld('wDay'), ;
                      sTime.fld('wHour'), sTime.fld('wMinute'), sTime.fld('wSecond') )
    ltBias = (ltSysTime - ltLocalTime)
    ltTimeToSet = ptDateTime + ltBias
*!*    ?'Year=',sTime.fld('wYear')  
*!*    ?'Month=',sTime.fld('wMonth')
*!*    ?'Day=',sTime.fld('wDay')    
*!*    ?'Hour=',sTime.fld('wHour')  
*!*    ?'Min=',sTime.fld('wMinute')
*!*    ?'Sec=',sTime.fld('wSecond')

    lnL=FileTimeToSystemTime(@lc_lwTime, @lc_sTime)
    sTime.Structure = lc_sTime
      sTime.fld('wYear')   = Year(ltTimeToSet)
      sTime.fld('wMonth')  = Month(ltTimeToSet)
      sTime.fld('wDay')    = Day(ltTimeToSet)
      sTime.fld('wHour')   = Hour(ltTimeToSet)
      sTime.fld('wMinute') = Minute(ltTimeToSet)
      sTime.fld('wSecond') = Sec(ltTimeToSet)
    lc_sTime = sTime.Structure
    lnL = SystemTimeToFileTime( @lc_sTime, @lc_lTime )
    lnL=SetFileTime(hFile, @lc_cTime, 0, @lc_lTime)
    lnL=CloseHandle(hFile)
  EndIf
  RETURN .T.
ENDPROC

*' OpenFile() Structure
DEFINE CLASS OFSTRUCT AS Struct
  PROCEDURE Init
    THIS.AddField( 'cBytes',     'BYTE',    0 )
    THIS.AddField( 'fFixedDisk', 'BYTE',    0 )
    THIS.AddField( 'nErrCode',   'INTEGER', 0 )
    THIS.AddField( 'Reserved1',  'INTEGER', 0 )
    THIS.AddField( 'Reserved2',  'INTEGER', 0 )
    THIS.AddField( 'szPathName', 'STRING',  REPL(chr(0),OFS_MAXPATHNAME) )
    THIS.fld('cBytes') = Len(THIS.Structure)
  ENDPROC
ENDDEFINE

DEFINE CLASS FILETIME AS Struct
  PROCEDURE Init
    THIS.AddField( 'dwLowDateTime', 'LONG', 0 )
    THIS.AddField( 'dwHighDateTime', 'LONG', 0 )
  ENDPROC
ENDDEFINE

DEFINE CLASS SYSTEMTIME AS Struct
  PROCEDURE Init
    THIS.AddField( 'wYear',         'INTEGER', 0 )
    THIS.AddField( 'wMonth',        'INTEGER', 0 )
    THIS.AddField( 'wDayOfWeek',    'INTEGER', 0 )
    THIS.AddField( 'wDay',          'INTEGER', 0 )
    THIS.AddField( 'wHour',         'INTEGER', 0 )
    THIS.AddField( 'wMinute',       'INTEGER', 0 )
    THIS.AddField( 'wSecond',       'INTEGER', 0 )
    THIS.AddField( 'wMilliseconds', 'INTEGER', 0 )
  ENDPROC
ENDDEFINE

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close