Announcement

Collapse
No announcement yet.

Need help with WinAPI

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with WinAPI

    Can someone give me an PB example of how to use the WinAPI function CompareFileTime ?

    I want to compare two files and find out which is older. I have not been able to get the syntax right for the API function.

  • #2
    It was mentioned here, if that helps...

    Comment


    • #3
      Originally posted by Chris Holbrook View Post
      It was mentioned here, if that helps...
      Yes, I saw that. But the actual PB call was not stated.

      This is what I have, maybe if someone can help clarify the variables....

      Declare Function CompareFileTime Lib "kernel32" ( _
      lpFileTime1 As FILETIME, _
      lpFileTime2 As FILETIME _
      ) As Long

      What do I put in lpFileTime1 and lpFileTime2 ?

      And what is FILETIME ?

      This result should be:

      -1 lpFileTime1 is smaller than lpFileTime2
      0 lpFileTime1 is equal to lpFileTime2
      +1 lpFileTime1 is larger than lpFileTime2

      I just don't know what I shoulld be using for the variables?

      Comment


      • #4
        Martin,

        There is nothing wrong with using PB's QUAD variables to do that, and if you do that, you will instantly have the information you need. But you would have to explicitly use BYVAL VARTPTR with the QUAD variables, because of the PB prototype in WIN32API.

        In re (it's been a long time since I had to look up the prototype, so this could be worng):

        GetFileTimes <filername>, BYVAL VARPTR(quad#1>, BYVAL VARTPTR(quad # 2), BYVAL VARPTR(quad

        Comment


        • #5
          MSDN filetime http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

          Comment


          • #6
            Originally posted by Martin Francom View Post
            Yes, I saw that. But the actual PB call was not stated.

            This is what I have, maybe if someone can help clarify the variables....

            Declare Function CompareFileTime Lib "kernel32" ( _
            lpFileTime1 As FILETIME, _
            lpFileTime2 As FILETIME _
            ) As Long

            What do I put in lpFileTime1 and lpFileTime2 ?

            And what is FILETIME ?

            This result should be:

            -1 lpFileTime1 is smaller than lpFileTime2
            0 lpFileTime1 is equal to lpFileTime2
            +1 lpFileTime1 is larger than lpFileTime2

            I just don't know what I shoulld be using for the variables?
            A FILETIME is a 64bit TYPE containing the date and time in 100nSec units
            since 1 Jan 1601 (UTC). Can be used for calculations and comparisions
            on Dates/Times other than files.

            What you put lpFileTime1 and lpFileTime2 is one of three times
            associated with a file (see example code).

            Code:
            #include "Win32API.inc"
            function CompFileTimes(FileName1 as string, FileName2 as string) as long
              local nFile1, nFile2 as dword 'PB file number
              local hFile1, hFile2 as dword 'Windows handle for file
              local ErrNum as long
              local FT1_Create, FT1_LastAccess, FT1_LastWrite as FileTime
              local FT2_Create, FT1_LastAccess, FT1_LastWrite as FileTime
            
              nFile1 = freefile
              open FileName1 for binary as #nFile1 'can be "for input", "for append,
              '{error checking left out}           '"for output" or "for random"
              nFile2 = freefile
              open FileName2 for binary as #nFile2
              '{error checking left out}
              hFile1 = fileattr(#nFile1, 2)
              hFile2 = fileattr(#nFile2, 2)
              ErrNum = GetFileTime hFile1, FT1_Create, FT1_LastAccess, _
                                                     FT1_LastWrite as FileTime
              '{error checking left out}
              ErrNum = GetFileTime hFile2, FT1_Create, FT2_LastAccess, _
                                                     FT2_LastWrite as FileTime
              '{error checking left out}
              function = CompareFileTime FT1_LastWrite, FT2_LastWrite
              close
            #if 0 '-----------logical equiv of CompareFileTime ------
              if FT1_LastWrite > FT2_LastWrite then
                function = 1
              elseif FT1_LastWrite < FT2_LastWrite then
                function = -1
              else
                function = 0
              end if
            #endif
            end function
            Have you looked msdn.microsoft.com ?
            Last edited by Dale Yarker; 16 Nov 2009, 02:05 AM. Reason: Added CLOSE to code
            Dale

            Comment


            • #7
              This is what I use to check to see if one file is new than another.

              First, get the FILETIME of each file:

              Code:
              Local diskFILETIME1, diskFILETIME1 As FILETIME
              Local sFilename1 As ASCIIZ * %MAX_PATH
              Local sFilename2 As ASCIIZ * %MAX_PATH
              
              ...
              ...
              ...
              
              DTSignature sFilename1, diskFILETIME1
              DTSignature sFilename2, diskFILETIME2
              
              
              Next, do the comparison:
              
              If CompareFileTime( diskFILETIME1, diskFILETIME1 ) <> 0 Then
              ..
              ..
              ..
              End If

              Code:
              '//
              '//  Determines the last update date/time of the specified file
              '//  The time stamp, as its raw FILETIME API value (64-bit) ; 0 on error
              '//  This function is purely to provide a way of telling when this value
              '//  changes for a file ; turning the value found into a human-readable
              '//  time is another matter altogether.
              '//
              Function DTSignature( ByRef zFileSpec As Asciiz, _
                                    RETURN_FILETIME As FILETIME _
                                    ) Export As Long
              
                Local hFile As Long
                Local uFind As WIN32_FIND_DATA
              
                'Locate the file...
                hFile = FindFirstFile( zFileSpec, uFind )
              
                If hFile Then
                  Return_FILETIME = uFind.ftLastWriteTime
                  FindClose hFile
                End If
              
              End Function
              Paul Squires
              FireFly Visual Designer (for PowerBASIC Windows 10+)
              Version 3 now available.
              http://www.planetsquires.com

              Comment


              • #8
                Dale & Paul,
                Thank you. Your explanations have been most helpful. If you can indulge me and answer a couple additional questions.
                Files have several dates: Date Created, Date modified, Date Accessed, and others. Which date do these routine use for making the comparison? I am hoping it's the date modified.

                If one of the files on which the comparison is going to be made is downloaded first from the intenet, will that affect the date comparison?

                Comment


                • #9
                  My code uses the last time the file was written to (uFind.ftLastWriteTime) so I guess it would be "Date Modified".

                  Check out:
                  Contains information about the file that is found by the FindFirstFile, FindFirstFileEx, or FindNextFile function. (ANSI)


                  If one of the files on which the comparison is going to be made is downloaded first from the intenet, will that affect the date comparison?
                  Not sure about an answer for that question. Not sure if the file date/time stamp gets modified when a file is downloaded. I guess the easiest way is to test it.
                  Paul Squires
                  FireFly Visual Designer (for PowerBASIC Windows 10+)
                  Version 3 now available.
                  http://www.planetsquires.com

                  Comment


                  • #10
                    Date Modified (LastWrite).

                    Internet - I think all three dates will be time of download. Dates for files inside a dot zip are preserved.

                    =============
                    added CLOSE in code in my previous post
                    Dale

                    Comment

                    Working...
                    X