Get and set file date and time stamps 
Author Message
 Get and set file date and time stamps


HF> I must apologise for the question I posed to you, actually I was
  > trying to get & set  the time and date of a file from Borland
  > TurboBasic, I figured that if I could follow your logic of getting
  > the size of a file then I could also figure how to get & set time
  > and date of a file. Well....... I try whole night and I gave out !

Ah!  That's a whole different ball game. Date and time stamps are tricky
because the fields are bit-encoded and you have to do some real register
juggling to make it work. Here's a couple of procedures I wrote in
powerbasic to do what you ask for. However they use the SHIFT statement
quite a lot and I'm not sure if TurboBASIC had that. If not maybe Dave
or someone can contribute a routine for doing integer shifts, it would
be useful for QuickBASIC as well.

--- cut here ---------------------------------------------------------------
' FILEDATE.BAS  get and set a files date and time stamps.
'
'   Author:     Christy Gemmell
'   Date:       3/7/1995
'   For:        Derek Sim
'   Compiler:   PowerBASIC
'
    DECLARE FUNCTION GetDateFormat% ()
    DECLARE FUNCTION GetFileDate$ (FileName$)
    DECLARE SUB SetFileDate (FileName$, FileDate$, Fmt%, Done%)

    CLS: PRINT: FileName$ = "PB.EXE"
    OldDate$ = GetFileDate$ (FileName$)
    IF OldDate$ <> "" THEN
       PRINT FileName$; " is currently dated "; OldDate$
       PRINT
       NewDate$ = LEFT$(DATE$, 6) + MID$(DATE$, 9, 2) + "  " + TIME$
       PRINT "Setting file to current date and time... ";
       CALL SetFileDate (FileName$, NewDate$, -1, Done%)
       IF Done% THEN
          PRINT "done"
          NewDate$ = GetFileDate$ (FileName$)
          PRINT
          PRINT FileName$; " is now dated "; NewDate$
          PRINT
          PRINT "Now reverting back to previous setting... ";
          CALL SetFileDate (FileName$, OldDate$, 0, Done%)
          IF Done% THEN
             PRINT "done"
             DateNow$ = GetFileDate$ (FileName$)
             PRINT
             PRINT FileName$; " is now dated "; DateNow$
          ELSE
             PRINT "failed!"
          END IF
       ELSE
          PRINT "failed!"
       END IF
    END IF
END

'   Returns date and time a file was last updated.
'
'   The date and time are returned as a string in one of these formats:
'
'       --123456789012345678--
'
'         MM-DD-YY  HH:MM:SS    (for USA)
'         DD/MM/YY  HH:MM:SS    (for Europe)
'         YY-MM-DD  HH:MM:SS    (for Japan)
'
'   (there are two blank spaces between the date and time
'
FUNCTION GetFileDate$ (FileName$)
    Dt$ = ""                            ' Assume failure
    F$ = FileName$ + CHR$(0)            ' Make filespec ASCIIZ
    REG 8, STRSEG(F$)                   ' DS = segment of filespec
    REG 4, STRPTR(F$)                   ' DX = offset of filespec
    REG 1, &H3D00                       ' DOS Service 61
    CALL INTERRUPT &H21                 ' - open file for reading
    Carry% = REG(0) AND 1               ' Check carry flag
    IF Carry% = 0 THEN                  ' If no error occurred..
       Handle% = REG(1)                 ' Get handle from AX
       REG 2, Handle%                   ' Transfer it to BX
       REG 1, &H5700                    ' DOS Service 87
       CALL INTERRUPT &H21              ' - get file date and time
       Carry% = REG(0) AND 1            ' Check carry flag
       IF Carry% = 0 THEN               ' If no error occurred..
          FlTime% = REG(3)              ' Bit-encoded time from CX
          FlDate% = REG(4)              ' Bit-encoded date from DX
          Yr% = FlDate%                 ' Extract
          SHIFT RIGHT Yr%, 9            '   the
          Yr% = Yr% + 1980              '     year
          FlDate% = FlDate% AND &H1FF   ' Isolate day and month
          Mth% = FlDate%                ' Extract
          SHIFT RIGHT Mth%, 5           '   the month
          Day% = FlDate% AND &H1F       ' Extract day
          Hrs% = FlTime%                ' Extract
          SHIFT RIGHT Hrs%, 11          '   hours
          FlTime% = FlTime% AND &H7FF   ' Isolate minutes and seconds
          Mins% = FlTime%               ' Extract
          SHIFT RIGHT Mins%, 5          '   minutes
          Sex% = (FlTime% AND &H1F) * 2 ' Extract seconds
          Y$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Yr%))), 2)
          M$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Mth%))), 2)
          D$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Day%))), 2)
(Continued to next message)

 * 1st 2.00o #323 * I ate a dozen oysters, but only nine of 'em worked.



Sat, 20 Dec 1997 03:00:00 GMT  
 Get and set file date and time stamps
(Continued from previous message)
          Fmt% = GetDateFormat%         ' Get national date format
          SELECT CASE Fmt%
              CASE 0                    ' USA
                   Dt$ = M$ + "-" + D$ + "-" + Y$
              CASE 1                    ' Europe
                   Dt$ = D$ + "/" + M$ + "/" + Y$
              CASE 2                    ' Japan
                   Dt$ = Y$ + "-" + M$ + "-" + D$
              CASE ELSE
          END SELECT
          H$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Hrs%))), 2)
          M$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Mins%))), 2)
          S$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Sex%))), 2)
          Dt$ = Dt$ + "  " + H$ + ":" + M$ + ":" + S$
       END IF
       REG 2, Handle%                   ' File handle to BX
       REG 1, &H3E00                    ' DOS Service 62
       CALL INTERRUPT &H21              ' - close the file
    END IF
    GetFileDate$ = Dt$                  ' Return date and time as string
END FUNCTION

'   Returns a code indicating the national date format.
'
'   Return values:  1 = MM-DD-YY   (USA)
'                   2 = DD/MM/YY   (Europe)
'                   3 = YY-MM-DD   (Japan)
'
'   Depends on COUNTRY = setting in CONFIG.SYS (default = USA)
'
FUNCTION GetDateFormat% ()
    B$ = SPACE$(64)                     ' To hold country information
    REG 8, STRSEG(B$)                   ' DS = segment of buffer
    REG 4, STRPTR(B$)                   ' DX = offset of buffer
    REG 1, &H3800                       ' DOS Service 56
    CALL INTERRUPT &H21                 ' - get country information
    GetDateFormat% = ASC(B$)            ' Date format is first byte
END FUNCTION

'   Sets the last-access date and time of the specified file.
'
'   Note: FileDate$ must be in one of the following formats:
'
'       --123456789012345678--
'
'         MM-DD-YY  HH:MM:SS    (for USA)
'         DD/MM/YY  HH:MM:SS    (for Europe)
'         YY-MM-DD  HH:MM:SS    (for Japan)
'
'   (there are two blank spaces between the date and time
'
'   If Fmt% is TRUE (non-zero) then the procedure uses the date
'   format for the country corresponding to the COUNTRY= setting
'   in the computers CONFIG.SYS file (default = USA)
'
'   If Fmt% is FALSE (zero) then USA format is used.
'
SUB SetFileDate (FileName$, FileDate$, Fmt%, Done%)
    Done% = 0                           ' Assume failure
    F$ = FileName$ + CHR$(0)            ' Make filespec ASCIIZ
    REG 8, STRSEG(F$)                   ' DS = segment of filespec
    REG 4, STRPTR(F$)                   ' DX = offset of filespec
    REG 1, &H3D00                       ' DOS Service 61
    CALL INTERRUPT &H21                 ' - open file for reading
    Carry% = REG(0) AND 1               ' Check carry flag
    IF Carry% = 0 THEN                  ' If no error occurred..
       Handle% = REG(1)                 ' Get handle from AX
       IF Fmt% THEN
          Fmt% = GetDateFormat%         ' Get national date format
       END IF
       SELECT CASE Fmt%
           CASE 0                       ' USA
                Day% = VAL(MID$(FileDate$, 4, 2))
                Mth% = VAL(LEFT$(FileDate$, 2))
                Yr% = VAL(MID$(FileDate$, 7, 2))
           CASE 1                       ' Europe
                Mth% = VAL(MID$(FileDate$, 4, 2))
                Day% = VAL(LEFT$(FileDate$, 2))
                Yr% = VAL(MID$(FileDate$, 7, 2))
           CASE 2                       ' Japan
                Mth% = VAL(MID$(FileDate$, 4, 2))
                Yr% = VAL(LEFT$(FileDate$, 2))
                Day% = VAL(MID$(FileDate$, 7, 2))
           CASE ELSE
       END SELECT
       Hrs% = VAL(MID$(FileDate$, 11, 2))
       Mins% = VAL(MID$(FileDate$, 14, 2))
       Sex% = VAL(MID$(FileDate$, 17, 2)) \ 2
       IF Yr% < 80 THEN Yr% = Yr% + 100 ' Remember the 21st Century
       FlDate& = Yr% - 80               ' Juggle date
       SHIFT LEFT FlDate&, 9            '   into the
       SHIFT LEFT Mth%, 5               '     appropriate
       FLDate& = FlDate& + Mth% + Day%  '       bit-fields
       REG 4, FlDate&                   ' Load result into DX
       FlTime& = Hrs%                   ' Juggle time
       SHIFT LEFT FlTime&, 11           '   into the
       SHIFT LEFT Mins%, 5              '     appropriate
       FlTime& = FlTime& + Mins% + Sex% '       bit-fields
       REG 3, FlTime&                   ' Load result into CX
       REG 2, Handle%                   ' File handle to BX
(Continued to next message)

 * 1st 2.00o #323 * I ate a dozen oysters, but only nine of 'em worked.



Sat, 20 Dec 1997 03:00:00 GMT  
 Get and set file date and time stamps
(Continued from previous message)
       REG 1, &H5701                    ' DOS Service 87
       CALL INTERRUPT &H21              ' - set file date and time
       Carry% = REG(0) AND 1            ' Check carry flag
       IF Carry% = 0 THEN               ' If no error occurred..
          Done% = -1                    '   report success
       END IF
       REG 2, Handle%                   ' File handle to BX
       REG 1, &H3E00                    ' DOS Service 62
       CALL INTERRUPT &H21              ' - close the file
    END IF
END SUB
--- and here ---------------------------------------------------------------

I've also added a little embellishment you might like, especially if
you're a foreigner like me. I've added a routine which, optionally,
formats the date in the style of the country the program is running in.
Not all of us use MM-DD-YY like these Americans.    =)

+------------------------+-----------------------------------------------+
|     _/_/_/_/  _/_/_/_/ | Christy Gemmell, Singular Software            |
|    _/        _/        | 11 Abingdon Road, Leicester LE2 1HA, England. |


| _/_/_/_/  _/_/_/_/     | Phone: +44-0116-254-7681                      |
+------------------------+-----------------------------------------------+

 * 1st 2.00o #323 * I ate a dozen oysters, but only nine of 'em worked.



Sat, 20 Dec 1997 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Setting file date/time stamp

2. Set File Date Time Stamp

3. How to set date/time stamp on a file

4. Setting File Date and Time stamps

5. Return File Date Time stamp

6. Access 7.0/8.0 (Win95) - How to retrieve a file's date/time stamp

7. Saving date/time stamp of text file

8. Determining file date-time stamp via HTTP

9. Modify File Date/Time Stamp with VB?

10. File's time/date stamp

11. changing date/time stamp on files

12. displaying file size, date and time stamps?

 

 
Powered by phpBB® Forum Software