Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Set File Time
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Titre:
Divers
Thread ID:
00088885
Message ID:
00089215
Vues:
23
>I have to set a file's creation time in Visual Foxpro 5 using
>Win32api function (CreateFile & SetFileTime).
>Please send me e-mail if you know how to declare the function.
>Thank you for your help!

Charles,

As I said in my reply to your email, here's how to do it. First, you'll need the following declarations and defines:
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
In the above, all SHORTs are actually Boolean values. Zero means the function failed.

The first thing you need to do is open the file to get a file handle:
lnhandle = CreateFile(@lcfile, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
In the above, lcfile is the fully qualified filename. If lnhandle is greater than zero, then the function succeeded.

Once the file is opened, you then need to create a SYSTEMTIME structure, then convert it to a FILETIME structure. The FILETIME structure, however, is in UTC time, and it has to be converted to local. Using a DateTime type (ltdate), that process would look like this:
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)
  IF llresult # 0
    lcresult = lcutctime
  ENDIF
ENDIF
RETURN lcresult

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
Once you have the FILETIME structures, call SetFileTime(). If you wish to leave any of the existing values unchanged, pass a zero, otherwise you'll need a structure for each of the date/times you wish to set. For example, a call setting the creation date would look like:
lnresult = SetFileTime(lnhandle, @lcreatetime, 0, 0)
hth,
George

Ubi caritas et amor, deus ibi est
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform