Use Windows
Use Dferror
Use Dll
Use Language.Inc

Define INVALID_HANDLE_VALUE For |CI-1
Define INVALID_FILE_SIZE    For |CI$FFFFFFFF
Define ERROR_NO_MORE_FILES  For |CI18

Define Max_Path For |CI260
Define MinChar  For |CI$80
Define MaxChar  For |CI$7F
Define MinShort For |CI$8000
Define MaxShort For |CI$7FFF
Define MinLong  For |CI$80000000
Define MaxLong  For |CI$7FFFFFFF
Define MaxByte  For |CI$FF
Define MaxWord  For |CI$FFFF
Define MaxDword For |CI$FFFFFFFF

// This function formats the time in a picture-string passed
//
// Picture  Meaning
//
//    h     Hours with no leading zero for single-digit hours; 12-hour clock
//    hh    Hours with leading zero for single-digit hours; 12-hour clock
//    H     Hours with no leading zero for single-digit hours; 24-hour clock
//    HH    Hours with leading zero for single-digit hours; 24-hour clock
//    m     Minutes with no leading zero for single-digit minutes
//    mm    Minutes with leading zero for single-digit minutes
//    s     Seconds with no leading zero for single-digit seconds
//    ss    Seconds with leading zero for single-digit seconds
//    t     One character time marker string, such as A or P
//    tt    Multicharacter time marker string, such as AM or PM
//
// For example, to get the time string
//    "11:29:40 PM"
// use the following picture string:
//    "hh':'mm':'ss tt"
External_Function GetTimeFormat "GetTimeFormatA" Kernel32.Dll ;
   Dword LCID Dword dwFlags Pointer lpsSystemTime Pointer lpFormat Pointer lpTimeStr Integer cchTime Returns Integer

// This function returns the time for the computer. It is the time of the clock shown in the taskbar of windows
External_Function GetLocalTime "GetLocalTime" Kernel32.Dll ;
   Pointer lpsSystemTime Returns Integer

// This function returns the time in GreenWhich currently based on the local time and the timezone information
External_Function GetSystemTime "GetSystemTime" Kernel32.Dll ;
   Pointer lpsSystemTime Returns Integer

//External_Function GetLastError "GetLastError" Kernel32.Dll ;
//   Returns Dword

// This function formats the date in a picture-string passed
//
// Picture  Meaning
//
//    d     Day of month as digits with no leading zero for single-digit days.
//    dd    Day of month as digits with leading zero for single-digit days.
//    ddd   Day of week as a three-letter abbreviation. The function uses the LOCALE_SABBREVDAYNAME value
//          associated with the specified locale.
//    dddd  Day of week as its full name. The function uses the LOCALE_SDAYNAME value associated with the
//          specified locale.
//    M     Month as digits with no leading zero for single-digit months.
//    MM    Month as digits with leading zero for single-digit months.
//    MMM   Month as a three-letter abbreviation. The function uses the LOCALE_SABBREVMONTHNAME value
//          associated with the specified locale.
//    MMMM  Month as its full name. The function uses the LOCALE_SMONTHNAME value associated with the
//          specified locale.
//    y     Year as last two digits, but with no leading zero for years less than 10.
//    yy    Year as last two digits, but with leading zero for years less than 10.
//    yyyy  Year represented by full four digits.
//    gg    Period/era string. The function uses the CAL_SERASTRING value associated with the specified
//          locale. This element is ignored if the date to be formatted does not have an associated era
//          or period string.
// For example, to get the date string
//    "Wed, Aug 31 94"
// use the following picture string:
//    "ddd',' MMM dd yy"
External_Function GetDateFormat "GetDateFormatA" Kernel32.Dll ;
   Dword LCID Dword dwFlags Pointer lpsSystemTime Pointer lpFormat Pointer lpDateStr Integer cchDate Returns Integer

Define LOCALE_NOUSEROVERRIDE   For |CI$80000000 // do not use user overrides
Define TIME_NOMINUTESORSECONDS For |CI$00000001 // do not use minutes or seconds
Define TIME_NOSECONDS          For |CI$00000002 // do not use seconds
Define TIME_NOTIMEMARKER       For |CI$00000004 // do not use time marker
Define TIME_FORCE24HOURFORMAT  For |CI$00000008 // always use 24 hour format

//
//  Date Flags for GetDateFormatW.
//
Define DATE_SHORTDATE        For |CI$00000001  // use short date picture
Define DATE_LONGDATE         For |CI$00000002  // use long date picture
Define DATE_USE_ALT_CALENDAR For |CI$00000004  // use alternate calendar (if any)

Type SystemTime
   Field SystemTime.wYear As Word
   Field SystemTime.wMonth As Word
   Field SystemTime.wDayOfWeek As Word
   Field SystemTime.wDay As Word
   Field SystemTime.wHour As Word
   Field SystemTime.wMinute As Word
   Field SystemTime.wSecond As Word
   Field SystemTime.wMilliseconds As Word
End_Type

Type FileTime
   Field FileTime.dwLowDateTime As Dword
   Field FileTime.dwHighDateTime As Dword
End_Type

Type Win32_Find_Data
   Field Win32_Find_Data.dwFileAttributes As Dword
   Field Win32_Find_Data.ftCreationLowDateTime As Dword
   Field Win32_Find_Data.ftCreationHighDateTime As Dword
   Field Win32_Find_Data.ftLastAccessLowDateTime As Dword
   Field Win32_Find_Data.ftLastAccessHighDateTime As Dword
   Field Win32_Find_Data.ftLastWriteLowDateTime As Dword
   Field Win32_Find_Data.ftLastWriteHighDateTime As Dword
   Field Win32_Find_Data.nFileSizeHigh As Dword
   Field Win32_Find_Data.nFileSizeLow As Dword
   Field Win32_Find_Data.dwReserved0 As Dword
   Field Win32_Find_Data.dwReserved1 As Dword
   Field Win32_Find_Data.cFileName As Char Max_Path
   Field Win32_Find_Data.cAlternateFileName As Char 14
End_Type

// lpFileTime     :pointer to file time to convert
// lpSystemTime   :pointer to structure to receive system time
External_Function FileTimeToSystemTime "FileTimeToSystemTime" Kernel32.Dll ;
   Pointer lpFileTime Pointer lpSystemTime Returns Integer

External_Function FileTimeToLocalFileTime "FileTimeToLocalFileTime" Kernel32.Dll ;
   Pointer lpFileTime Pointer lpSystemTime Returns Integer

// lpFileName      :address of name of file to search for
// lpFindFileData  :address of returned information
External_Function FindFirstFile "FindFirstFileA" Kernel32.Dll ;
   Pointer lpFileName Pointer lpFindFileData Returns Handle

// hFindFile       :handle of search
// lpFindFileData  :address of structure for data on found file
External_Function FindNextFile "FindNextFileA" Kernel32.Dll ;
   Handle hFindFile Pointer lpFindFileData Returns Integer

// hFindFile       :file search handle
External_Function FindClose "FindClose" Kernel32.Dll ;
   Handle hFindFile Returns Integer

Function ConvertDateTimeToSystemDateTime Global Dword dwLowDateTime Dword dwHighDateTime Returns String
   String sftTime sSystemTime sFormattedTime sFormattedDate sLocalFileTime
   Pointer lpsftTime lpsSystemTime lpsFormattedTime lpsFormattedDate lpsLocalFileTime
   Integer iSuccess iLenCcTime iDataLength iLenCcDate

   ZeroType FileTime To sftTime
   Put dwLowDateTime To sftTime At FileTime.dwLowDateTime
   Put dwHighDateTime To sftTime At FileTime.dwHighDateTime
   GetAddress Of sftTime To lpsftTime

   ZeroType SystemTime To sSystemTime
   GetAddress Of sSystemTime To lpsSystemTime

   Move (FileTimeToSystemTime (lpsftTime, lpsSystemTime)) To iSuccess
   If (iSuccess <> 0) Begin
      ZeroString 255 To sFormattedTime
      GetAddress Of sFormattedTime To lpsFormattedTime
      Length sFormattedTime To iLenCcTime
      Move (GetTimeFormat (Locale_User_Default, 0, lpsSystemTime, 0, lpsFormattedTime, iLenCcTime)) To iDataLength

      ZeroString 255 To sFormattedDate
      GetAddress Of sFormattedDate To lpsFormattedDate
      Length sFormattedDate To iLenCcDate
      Move (GetDateFormat (Locale_User_Default, 0, lpsSystemTime, 0, lpsFormattedDate, iLenCcDate)) To iDataLength

      Function_Return (Cstring (sFormattedDate) * Cstring (sFormattedTime))
   End
End_Function // ConvertDateTimeToSystemDateTime

Function ConvertDateTimeToFileDateTime Global Dword dwLowDateTime Dword dwHighDateTime Returns String
   String sftTime sSystemTime sFormattedTime sFormattedDate sLocalFileTime
   Pointer lpsftTime lpsSystemTime lpsFormattedTime lpsFormattedDate lpsLocalFileTime
   Integer iSuccess iLenCcTime iDataLength iLenCcDate

   ZeroType FileTime To sftTime
   Put dwLowDateTime To sftTime At FileTime.dwLowDateTime
   Put dwHighDateTime To sftTime At FileTime.dwHighDateTime
   GetAddress Of sftTime To lpsftTime

   ZeroType FileTime To sLocalFileTime
   GetAddress Of sLocalFileTime To lpsLocalFileTime

   Move (FileTimeToLocalFileTime (lpsftTime, lpsLocalFileTime)) To iSuccess
   If (iSuccess <> 0) Begin
      ZeroType SystemTime To sSystemTime
      GetAddress Of sSystemTime To lpsSystemTime

      Move (FileTimeToSystemTime (lpsLocalFileTime, lpsSystemTime)) To iSuccess
      If (iSuccess <> 0) Begin
         ZeroString 255 To sFormattedTime
         GetAddress Of sFormattedTime To lpsFormattedTime
         Length sFormattedTime To iLenCcTime
         Move (GetTimeFormat (Locale_User_Default, 0, lpsSystemTime, 0, lpsFormattedTime, iLenCcTime)) To iDataLength

         ZeroString 255 To sFormattedDate
         GetAddress Of sFormattedDate To lpsFormattedDate
         Length sFormattedDate To iLenCcDate
         Move (GetDateFormat (Locale_User_Default, 0, lpsSystemTime, 0, lpsFormattedDate, iLenCcDate)) To iDataLength

         Function_Return (Cstring (sFormattedDate) * Cstring (sFormattedTime))
      End
   End
End_Function // ConvertDateTimeToFileDateTime

Procedure DoBrowseDir String sFilePath
   String sWin32FindData
   String sShortFileName sLongFileName
   Pointer lpsFilePath lpsWin32FindData
   Handle hFindFile
   Dword dwFileAttributes dwFileSizeHigh dwFileSizeLow
   Dword dwLowDateTime dwHighDateTime
   Integer iVoid iRetval

   GetAddress Of sFilePath To lpsFilePath

   ZeroType Win32_Find_Data To sWin32FindData
   GetAddress Of sWin32FindData To lpsWin32FindData

   Move (FindFirstFile (lpsFilePath, lpsWin32FindData)) To hFindFile
   If hFindFile Ne INVALID_HANDLE_VALUE Begin
      Repeat
         GetBuff_String From sWin32FindData At Win32_Find_Data.cFileName To sLongFileName
         Showln "Long filename: " sLongFileName

         GetBuff_String From sWin32FindData At Win32_Find_Data.cAlternateFileName To sShortFileName
         Showln "   short filename: " (Cstring (sShortFileName))

         GetBuff From sWin32FindData At Win32_Find_Data.dwFileAttributes To dwFileAttributes
         Showln "   dwFileAttributes: " dwFileAttributes

         GetBuff From sWin32FindData At Win32_Find_Data.nFileSizeHigh To dwFileSizeHigh
         GetBuff From sWin32FindData At Win32_Find_Data.nFileSizeLow To dwFileSizeLow
         Showln "   file size: " ((dwFileSizeHigh * MaxDword) + dwFileSizeLow)

         Showln "Times in UTC"
         GetBuff From sWin32FindData At Win32_Find_Data.ftCreationLowDateTime To dwLowDateTime
         GetBuff From sWin32FindData At Win32_Find_Data.ftCreationHighDateTime To dwHighDateTime
         Showln "   Created: " (ConvertDateTimeToSystemDateTime (dwLowDateTime, dwHighDateTime))

         GetBuff From sWin32FindData At Win32_Find_Data.ftLastAccessLowDateTime To dwLowDateTime
         GetBuff From sWin32FindData At Win32_Find_Data.ftLastAccessHighDateTime To dwHighDateTime
         Showln "   Last access: " (ConvertDateTimeToSystemDateTime (dwLowDateTime, dwHighDateTime))

         GetBuff From sWin32FindData At Win32_Find_Data.ftLastWriteLowDateTime To dwLowDateTime
         GetBuff From sWin32FindData At Win32_Find_Data.ftLastWriteHighDateTime To dwHighDateTime
         Showln "   Last write: " (ConvertDateTimeToSystemDateTime (dwLowDateTime, dwHighDateTime))

         Showln "Times in CURRENT POSITION"
         GetBuff From sWin32FindData At Win32_Find_Data.ftCreationLowDateTime To dwLowDateTime
         GetBuff From sWin32FindData At Win32_Find_Data.ftCreationHighDateTime To dwHighDateTime
         Showln "   Created: " (ConvertDateTimeToFileDateTime (dwLowDateTime, dwHighDateTime))

         GetBuff From sWin32FindData At Win32_Find_Data.ftLastAccessLowDateTime To dwLowDateTime
         GetBuff From sWin32FindData At Win32_Find_Data.ftLastAccessHighDateTime To dwHighDateTime
         Showln "   Last access: " (ConvertDateTimeToFileDateTime (dwLowDateTime, dwHighDateTime))

         GetBuff From sWin32FindData At Win32_Find_Data.ftLastWriteLowDateTime To dwLowDateTime
         GetBuff From sWin32FindData At Win32_Find_Data.ftLastWriteHighDateTime To dwHighDateTime
         Showln "   Last write: " (ConvertDateTimeToFileDateTime (dwLowDateTime, dwHighDateTime))

         Move (FindNextFile (hFindFile, lpsWin32FindData)) To iRetval
         // If iRetval = false the function call fails but GetLastError fails to tell me if
         // this was because we do not find any files anymore.
      Until iRetval Eq Dffalse

      Move (FindClose (hFindFile)) To iVoid
   End
End_Procedure // DoBrowseDir

Send DoBrowseDir "*.VD7"
Send Info_Box "Ready"