Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
How to change display settings: screen resolution, screen refresh rate
Enumerating data formats currently available on the clipboard
Custom GDI+ class
Capturing keyboard activity of another application with the Raw Input API (VFP9)
Mapping and disconnecting network drives
Winsock: retrieving directory listing from an FTP server using passive data connection (FTP, port 21)
Winsock: sending email messages (SMTP, port 25)
How to display the Properties dialog box for a file (ShellExecuteEx)
Disk in drive A:
Enumerating raw input devices attached to the system (keyboard, mouse, human interface device)
How to download a file from the FTP server using FtpGetFile
Using Font and Text functions
Detecting changes in connections to removable drives (VFP9)
How to play AVI file on the _screen
Using Video Capture: displaying on FoxPro form frames and previewing video obtained from a digital camera
Using EnumPrinters function to enumerate locally installed printers
Splash Screen for the VFP application
Running MSDOS Shell as a child process with redirected input and output (smarter RUN command)
Converting Unicode data from the Clipboard to a character string using a given code page
Creating irregularly shaped FoxPro form using transparency color key
Subclassing CommandButton control to create BackColor property
Enumerating network resources
Vertical Label control
Custom HttpRequest class (WinHTTP)
HOWTO: Use the Win32 API to Access File Dates and Times

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:
This sample code originates from HOWTO: Use the Win32 API to Access File Dates and Times

Unfortunately two regrettable errors make the original code unusable: missing @ character in GetFileTime call, and hex2dec function, never being tested I guess. So I was bold enough to make some fixes to it:
 
*********************************************************
* Function getftime
* Return the create date and time, last access date and time,
* last write date and time from a passed filename.
* 
* Passed:
* lcFileName (by value): fully-qualified filename
* 
* Returns:
* Logical .t. if completed successfully, .f. if not.
* 
* Return variables:
* ldCreateDate (date, by reference): Creation date
* lcCreateTime (character, by reference): Creation time (HH:MM:SS.mm)
* ldAccessDate (date, by reference): Last access date
* lcAccessTime (character, by reference): Last access time (HH:MM:SS.mm)
* lcLastWriteDate (date, by reference): Last write date
* lcLastWriteTime (character, by reference): Last write time (HH:MM:SS.mm)
 
STORE {} TO m.createdate, m.accessdate, m.lastwritedate
STORE "" TO m.createtime, m.accesstime, m.lastwritetime
 
m.OK = getftime("C:\Test.txt", @m.createdate, @m.createtime, ;
    @m.accessdate, @m.accesstime, @m.lastwritedate, @m.lastwritetime)
 
IF m.OK
    ? "Create date:      ", m.createdate
    ? "Create time:      " , m.createtime
    ? "Last access date: ", m.accessdate
    ? "Last access time: ", m.accesstime
    ? "Last write date:  ", m.lastwritedate
    ? "Last write time:  ", m.lastwritetime
ELSE
    ? "Error getting file times"
ENDIF
* End of usage example.
 
FUNCTION getftime
PARAMETERS lcFileName, ldCreateDate, lcCreateTime, ldAccessDate,;
    lcAccessTime, ldLastWriteDate, lcLastWriteTime
 
* DEFINEs for file share mode.
#DEFINE FILE_SHARE_READ  1
#DEFINE FILE_SHARE_WRITE 2
 
* DEFINEs for access right
 #DEFINE GENERIC_READ  hex2dec("80000000")
 #DEFINE GENERIC_WRITE hex2dec("40000000")
 
* DEFINES for Create Mode.
#DEFINE CREATE_NEW  1
#DEFINE CREATE_ALWAYS 2
#DEFINE OPEN_EXISTING 3
#DEFINE OPEN_ALWAYS 4
#DEFINE TRUNCATE_EXISTING 5
 
* File flag.
#DEFINE FILE_ATTRIBUTE_NORMAL hex2dec("80")
 
    * Blank filename or nonexistent file, no need to continue.
    IF EMPTY(lcFileName) OR NOT FILE(lcFileName)
        RETURN .F.
    ENDIF
 
    DECLARE INTEGER CreateFile IN kernel32;
        STRING lpFileName, INTEGER dwDesiredAccess,;
        INTEGER dwShareMode, INTEGER lpSecurityAttributes,;
        INTEGER dwCreationDisposition,;
        INTEGER dwFlagsAndAttributes, INTEGER hTemplateFile
 
    DECLARE INTEGER GetFileTime IN kernel32;
        INTEGER hFile, STRING @lpCreationTime,;
        STRING @lpLastAccessTime, STRING @lpLastWriteTime
 
    DECLARE INTEGER FileTimeToLocalFileTime IN kernel32;
        STRING @lpFileTime, STRING @lpLocalFileTime
 
    DECLARE INTEGER FileTimeToSystemTime IN kernel32;
        STRING @lpFileTime, STRING @lpSystemTime
 
    DECLARE INTEGER GetLastError IN kernel32
    DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject
 
    * 2 DWORDs * 4 bytes each = an 8-byte buffer.
    STORE SPACE(8) TO CreationTime, LastAccessTime, lastwritetime
 
    * Same buffers for local time.
    STORE SPACE(8) TO LocalCreationTime,;
        LocalLastAccessTime, LocalLastWriteTime
 
    * 16-byte buffers for when we convert from the FILETIME
    * structs to the more human-readable SYSTEMTIME structures.
    STORE SPACE(16) TO ctSystemTime, laSystemTime, lwSystemTime
 
    * Open an existing file.
    lhFileHandle = CreateFile(lcFileName, GENERIC_READ,;
        FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
 
    IF lhFileHandle <= 0
        =MESSAGEB ("Error in CreateFile - " + lcFileName + ": Error code - " + ;
            LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
    * Make the GetFileTime call, to get Create, Access and Update times.
    m.retval = GetFileTime (lhFileHandle,;
        @CreationTime, @LastAccessTime, @lastwritetime)
 
    IF m.retval = 0
    * 32 = Sharing violation
    * 87 = Invalid parameter
        =MESSAGEB("Error calling GetFileTime. Error code: " + ;
            LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
* Successfully performed the GetFileTime call -
* now convert each of the three times to local time.
* If any of them fail, then exit.
    IF     FileTimeToLocalFileTime (@CreationTime, @LocalCreationTime) = 0;
        OR FileTimeToLocalFileTime (@LastAccessTime,@LocalLastAccessTime) = 0;
        OR FileTimeToLocalFileTime (@lastwritetime, @LocalLastWriteTime) = 0 
 
        = MESSAGEB("FileTimeToLocalFileTime Failed. Error code: " + ;
            LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
* Now you have a local filetime, convert each to a systemtime that
* you can work with.
* If any of the calls fail, then exit.
    IF     FileTimeToSystemTime (@LocalCreationTime, @ctSystemTime) = 0;
        OR FileTimeToSystemTime (@LocalLastAccessTime, @laSystemTime) = 0;
        OR FileTimeToSystemTime (@LocalLastWriteTime, @lwSystemTime) = 0
 
        = MESSAGEB("FileTimeToSystemTime call failed. Error code: " ;
            + LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
    ctstYear = Str2Word(LEFT(ctSystemTime, 2))
    ctstMonth = Str2Word(SUBSTR(ctSystemTime, 3, 2))
    ctstDayofWeek = Str2Word(SUBSTR(ctSystemTime, 5, 2))
    ctstDay = Str2Word(SUBSTR(ctSystemTime, 7, 2))
    ctstHour = Str2Word(SUBSTR(ctSystemTime, 9, 2))
    ctstMinute = Str2Word(SUBSTR(ctSystemTime, 11, 2))
    ctstSecond = Str2Word(SUBSTR(ctSystemTime, 13, 2))
    ctstMilliSecond = Str2Word(SUBSTR(ctSystemTime, 15, 2))
 
* Unpack the integers in last access time SystemTime structure.
    lastYear = Str2Word(LEFT(laSystemTime, 2))
    lastMonth = Str2Word(SUBSTR(laSystemTime, 3, 2))
    lastDayofWeek = Str2Word(SUBSTR(laSystemTime, 5, 2))
    lastDay = Str2Word(SUBSTR(laSystemTime, 7, 2))
    lastHour = Str2Word(SUBSTR(laSystemTime, 9, 2))
    lastMinute = Str2Word(SUBSTR(laSystemTime, 11, 2))
    lastSecond = Str2Word(SUBSTR(laSystemTime, 13, 2))
    lastMilliSecond = Str2Word(SUBSTR(laSystemTime, 15, 2))
 
* Unpack the integers in last write time SystemTime structure.
    lwstYear = Str2Word(LEFT(lwSystemTime, 2))
    lwstMonth = Str2Word(SUBSTR(lwSystemTime, 3, 2))
    lwstDayofWeek = Str2Word(SUBSTR(lwSystemTime, 5, 2))
    lwstDay = Str2Word(SUBSTR(lwSystemTime, 7, 2))
    lwstHour = Str2Word(SUBSTR(lwSystemTime, 9, 2))
    lwstMinute = Str2Word(SUBSTR(lwSystemTime, 11, 2))
    lwstSecond = Str2Word(SUBSTR(lwSystemTime, 13, 2))
    lwstMilliSecond = Str2Word(SUBSTR(lwSystemTime, 15, 2))
 
* Close the file handle.
    llRetCode = CloseHandle(lhFileHandle)
 
* Format the return variables.
    ldCreateDate = CTOD(LTRIM(STR(ctstMonth)) + "/" + ;
        LTRIM(STR(ctstDay)) + "/" + LTRIM(STR(ctstYear)))
 
    lcCreateTime = PADL(LTRIM(STR(ctstHour)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(ctstMinute)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(ctstSecond)), 2, "0") + "." + ;
        PADL(LTRIM(STR(ctstMilliSecond)), 3, "0")
 
    ldAccessDate = CTOD(LTRIM(STR(lastMonth)) + "/" + ;
        LTRIM(STR(lastDay)) + "/" + LTRIM(STR(lastYear)))
 
    lcAccessTime = PADL(LTRIM(STR(lastHour)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lastMinute)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lastSecond)), 2, "0") + "." + ;
        PADL(LTRIM(STR(lastMilliSecond)), 3, "0")
 
    ldLastWriteDate = CTOD(LTRIM(STR(lwstMonth))+ ;
        "/"+LTRIM(STR(lwstDay)) + "/" + LTRIM(STR(lwstYear)))
 
    lcLastWriteTime = PADL(LTRIM(STR(lwstHour)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lwstMinute)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lwstSecond)), 2, "0") + "." + ;
        PADL(LTRIM(STR(lwstMilliSecond)), 3, "0")
 
* Function hex2dec - Convert a character hex value into decimal.
* Handy for dealing with Hex declarations in API calls.
* Passed: m.hex (character hex value)
* Returns: Decimal value
FUNCTION hex2dec
PARAMETER m.hex
    m.decimal = 0
    FOR i = LEN(m.hex) TO 1 STEP -1
        m.decimal = m.decimal + (AT(SUBSTR(m.hex, i, 1), ;
            "0123456789ABCDEF") - 1) * 16 ^ (LEN(m.hex) - i)
    NEXT
RETURN m.decimal
 
* Function str2word - Converts low-high format string representation
* to a 16-bit integer (word) value. Useful for unrolling structure
* members containg WORD types.
* 
* Passed: Low-high string representation of 16-bit integer
* Returns: numeric value
FUNCTION Str2Word
PARAMETERS m.wordstr
    PRIVATE i, m.retval
 
    m.retval = 0
    FOR i = 0 TO 8 STEP 8
        m.retval = m.retval + (ASC(m.wordstr) * (2^i))
        m.wordstr = RIGHT(m.wordstr, LEN(m.wordstr) - 1)
    NEXT
RETURN m.retval
 
 

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
8038 bytes  
Created: 2001-10-28 10:13:20  
Modified: 2007-05-04 08:01:54  
Visits in 7 days: 136  
Listed functions:
CloseHandle
CreateFile
FileTimeToLocalFileTime
FileTimeToSystemTime
GetFileTime
GetLastError
Printer friendly API declarations
My comment:
It seems that calling MODIFY FILE in VFP6 completely removes the previous file state, because the file creation time changes to become equivalent to the last write time. This is in contrast to the Notepad editing, which preserves the original creation datetime value.

FoxPro ADIR() function places information about files into an array and then returns the number of files. It returns only date and time the file was last modified.

In C# version, I use managed FileInfo object instead of accessing file times through the System.Runtime.InteropServices namespace.
Word Index links for this example:
Translate this page:
  Spanish    Portuguese    German    French    Italian  
FreeTranslation.com offers instant, free translations of text or web pages.
User Contributed Notes:
There are no notes on this subject.


Copyright © 2001-2013 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

Privacy policy
Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (23.22.212.158)
4 sec.Function: 'SHEnumKeyEx'
7 sec.Example: 'List of addresses in the AutoDial mapping database'
6.89 hrs.Function: 'EnumProcessModules'
 Example: 'Testing if a connection to an Url can be established'
 Example: 'Retrieving long values associated with the class of the VFP window'
8.7 hrs.Example: 'Using Multimedia Command Strings to play MIDI files'
 Example: 'Reading the structure of VFP main menu'
 Example: 'Copying picture of the active form to the Clipboard using Enhanced Metafile API functions'
10.89 hrs.Solution: 'LanguageBar ActiveX Control'
15.96 hrs.Example: 'How to check whether the system is 32-bit or 64-bit'
Google
Advertise here!