'===========================================================================
' Subject: SET A FILE'S DATE & TIME           Date: 09-22-98 (15:03)       
'  Author: Don Schullian                      Code: PBCC                   
'  Origin: d83@ath.forthnet.gr              Packet: PBCC.ABC
'===========================================================================
'  Here's a bit of a routine that may come in handy from time to time. It will
'allow you to set a file's time and date. Now-a-days there are, actually 3
'TIME:DATE stamps for each file but this routine only changes/sets the one that
'is visable to users. Modifying the function to make changes to the other two
'would be just a matter of which member name of tDTA that you choose to use.

'  Anyhow, I'm not sure I understand half of what I'm doing here so make SURE
'that the time and date info you send is valid or NULL if you don't wish to
'change that particular item. Also, if you're not going to send the date and
'time in DATE$ and TIME$ format then you'll have to modify appropriate lines in
'the function to accomodate your individual method.

' If, by chance you run into any 'anomolies' (read bugs) please give me a
'shout.

'=============================================================================
'========================  START CODE ========================================
'=============================================================================
$INCLUDE "WIN32API.INC"
  DECLARE FUNCTION fSetFileTD(BYVAL FileSpec AS STRING,BYVAL Dyte AS STRING, BYVAL Tyme AS STRING) AS LONG

FUNCTION PBmain () AS LONG

  LOCAL Dyte     AS STRING
  LOCAL FileSpec AS STRING
  LOCAL Tyme     AS STRING

  FileSpec = ""
  Dyte     = "04-04-1994"          ' mm-dd-yyyy
  Tyme     = "04:04:04"            ' hh:mm:ss

  IF ISTRUE fSetFileTD(FileSpec,Dyte,Tyme) THEN
      PRINT "Success"
    ELSE
      PRINT "Failed"
  END IF
  WAITKEY$

END FUNCTION
'=============================================================================
'======================   END TEST CODE  =====================================
'=============================================================================
$IF 0
    ----------------------------                      PowerBASIC/cc v1.0
 ---|          DASoft          |------------------------------------------
    ----------------------------    Copyright 1998      DATE: 1998-09-22
    | FILE NAME   File-TD .bas |          by
    |                          |  Don Schullian, Jr.
    ----------------------------
  PURPOSE: Set a file's time and date
   PARAMS: FileSpec$ Drive:\Path\FileName.ext of file to be changed
           Dyte$     IF NOT NULL then "mm-dd-yyy" format
           Tyme$     IF NOT NULL then "hh:mm:ss"  format
   RETURN: 1 IF Successful
           NULL if failed
 --------------------------------------------------------------------------
$ENDIF

FUNCTION fSetFileTD (BYVAL FileSpec AS STRING, _
                     BYVAL Dyte     AS STRING, _
                     BYVAL Tyme     AS STRING  ) AS LONG

  LOCAL zFileSpec   AS ASCIIz * 260                          ' zSTRING for Windows
  LOCAL  FileNbr    AS LONG                                  ' PBcc's file number
  LOCAL  FileHandle AS LONG                                  ' Windows' file handle
  LOCAL tDTA        AS WIN32_FIND_DATA                       ' The file's info
  LOCAL tST         AS SystemTime                            ' unpacked time:date info
                                                             '
  ON ERROR GOTO fSetFileTDoops                               ' set error trap
                                                             '
  zFileSpec  = FileSpec                                      ' to zSTRING
  FileHandle = FindFirstFile( zFileSpec, tDTA )              ' get file info
  IF FileHandle < 1 THEN EXIT FUNCTION                       ' oops! no file here
  FindClose FileHandle                                       ' close find routine
                                                             '
  FileNbr = FREEFILE                                         ' next PBcc file number
  OPEN FileSpec FOR BINARY AS FileNbr                        ' open the file
  FileHandle = FILEATTR(FileNbr,2)                           ' get Win's file handle
  IF ( LEN(Dyte) = 0 )   OR _                                ' if either date or time
     ( LEN(Tyme) = 0 ) THEN                                  '  is to stay the same then
    FileTimeToLocalFileTime tDTA.ftLastWriteTime, _          '  convert info into local time
                            tDTA.ftLastWriteTime             '  and then into system time
    FileTimeToSystemTime    tDTA.ftLastWriteTime, tST        '
  END IF                                                     '
  IF LEN(Dyte) > 0 THEN                                      ' if we're setting the date
    tST.wYear      = VAL(MID$(Dyte,7))                       '  set members with values
    tST.wMonth     = VAL(MID$(Dyte,1))                       '
    tST.wDay       = VAL(MID$(Dyte,4))                       '
    tST.wDayOfWeek = 0                                       '  (not necessary)
  END IF                                                     '
  IF LEN(Tyme) > 0 THEN                                      ' if we're setting the file time
    tST.wHour         = VAL(MID$(Tyme,1))                    '  set members with values
    tST.wMinute       = VAL(MID$(Tyme,3))                    '
    tST.wSecond       = VAL(MID$(Tyme,7))                    '
    tST.wMilliSeconds = 0                                    '  (really!)
  END IF                                                     '
  SystemTimeToFileTime tST, tDTA.ftLastWriteTime             ' convert to local file time
  LocalFileTimeToFileTime tDTA.ftLastWriteTime, _            ' convert to system file time
                          tDTA.ftLastWriteTime               '
                                                             '
  FUNCTION = SetFileTime(FileHandle,tDTA.ftCreationTime  , _ ' set/make changes &
                                    tDTA.ftLastAccessTime, _ ' RETURN ZERO or 1
                                    tDTA.ftLastWriteTime   ) '
                                                             '
  fSetFileTDexit:                                            ' We're outt'a here
    IF FILEATTR(FileNbr,0) THEN CLOSE #FileNbr               '  if the file is actually open
    EXIT FUNCTION                                            '  bye!
                                                             '
  fSetFileTDoops:                                            ' OOPS!
    RESUME fSetFileTDexit                                    '

END FUNCTION
