Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Setting a files date and time stamp in VFP
Message
De
16/09/1999 06:35:57
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
15/09/1999 17:23:13
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Divers
Thread ID:
00265216
Message ID:
00265368
Vues:
15
>Does anyone know how to set a files date and time stamp in VFP?


Mark,
I paste the code here too for others' benefit.
Original code is not mine but I don't remember who to credit. I modified the code slightly :
handle=fcreate("test.txt")
if handle > 0
	=fwrite(handle,"Hello",5)
	=fclose(handle)
	=_SetFileTime(sys(5)+curdir()+"test.txt",dtot(date()-30)+3600*5) && 30 days before,5 AM
	adir(aFileTime,"test.txt")
	erase test.txt
	disp memo like aFileTime
endif	

function _SetFileTime
LPARAMETERS tcFile, ttNewTime
IF empty(ttNewTime)
  MESSAGEBOX("Invalid date passed")
  RETURN .f.
ENDIF
DECLARE SHORT SetFileTime IN Win32API;
  INTEGER hFile, STRING @lpftCreation,;
  STRING @lpftLastAccess, STRING @lpftLastWrite
DECLARE SHORT SystemTimeToFileTime IN Win32API;
  STRING @lpst,	STRING @lpft
DECLARE SHORT LocalFileTimeToFileTime IN Win32API;
  STRING @lpLocalFileTime, STRING @lpFileTime
DECLARE INTEGER CreateFile IN Win32API;
  STRING @lpFileName, INTEGER dwDesiredAccess,;
  INTEGER dwShareMode, STRING @lpSecurityAttributes,;
  INTEGER dwCreationDistribution, INTEGER dwFlagsAndAttributes,;
  INTEGER hTemplateFile
DECLARE SHORT CloseHandle IN Win32API;
  INTEGER hObject
#DEFINE GENERIC_WRITE    0x40000000
#DEFINE FILE_SHARE_WRITE 0x00000002
#DEFINE OPEN_EXISTING    0x00000003

lnhandle = CreateFile(@tcFile, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0) && Create handle
IF lnhandle > 0
  lcreatetime = makefiletime(ttNewTime) && Create a filetime structure
  IF empty(lcreatetime)
    MESSAGEBOX("Filetime creation failed !")
    =CloseHandle(lnhandle) && Close the handle
    RETURN .f.
  ENDIF
  lnresult = SetFileTime(lnhandle, @lcreatetime, @lcreatetime, @lcreatetime) && Set file time
  =CloseHandle(lnhandle) && Close the handle
  RETURN (lnresult # 0)
ENDIF
MESSAGEBOX("Cannot create handle to file!")
RETURN .f.

FUNCTION makefiletime
  LPARAMETER ltdate
  LOCAL lnyear, lnmonth, lndow, lnday, lnhours,;
    lnminute, lnsecond, lnmilli, lnresult, lcresult
  lnyear = YEAR(ltdate)
  lnmonth = MONTH(ltdate)
  lndow = DOW(ltdate)
  lnday = DAY(ltdate)
  lnhours = HOUR(ltdate)
  lnminute = MINUTE(ltdate)
  lnsecond = SEC(ltdate)
  lnmilli = 0
  lcsystime = Val2Str(lnyear, 2) + Val2Str(lnmonth, 2) +;
    Val2Str(lndow, 2) + Val2Str(lnday, 2) +;
    Val2Str(lnhours, 2) + Val2Str(lnminute, 2) +;
    Val2Str(lnsecond, 2) + Val2Str(lnmilli, 2)
  STORE REPLICATE(CHR(0), 8) TO lcfiletime, lcutctime
  lnresult = SystemTimeToFileTime(@lcsystime, @lcfiletime)
  IF lnresult # 0
    lnresult = LocalFileTimeToFileTime(@lcfiletime, @lcutctime)
    RETURN iIF( lnresult # 0, lcutctime, "" )
  ENDIF
  RETURN ""

FUNCTION Val2Str
  LPARAMETERS pn_val, pn_bytes

  LOCAL lcresult, lnval, lnmask, lni, lnbyte
  lcresult = ""
  lnval = pn_val
  lnmask = 255
  FOR lni = 1 TO pn_bytes
    lnbyte = BITAND(lnval, lnmask)
    lcresult = lcresult + CHR(lnbyte)
    lnval = BITRSHIFT(lnval, 8)
  NEXT
  RETURN lcresult
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform