Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change a File's Date and Time? (ie. "Touch")

API Functions

Change a File's Date and Time? (ie. "Touch")

by  wgcs  Posted    (Edited  )
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... )
Code:
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top