Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
URL: splitting into its component parts
Drawing icons associated with the VFP main window
Finding the path to the VFP executable running
Scanning a hierarchy of child windows down from the Windows Desktop
Disabling drawing in the VFP form
Enumerating Processes -- WinNT
Enumerating Volumes and Volume Mounting Points (NTFS)
Extracting the name and extension parts of a path string
Form Magnifier
How to display picture stored in enhanced-format metafile (*.emf)
How to position the GETPRINTER() dialog
Listing INF files in a specified directory
Retrieving top-child window for the VFP form
Testing an ODBC connection for supporting specific functionality
Comparing dimensions of the VFP main window with _SCREEN properties
Reading and setting the priority class values for the current process and thread
Reading entries from Event logs
Retrieving list of files on the FTP directory
Using the RestartDialog function -- restarting Windows
How to obtain the number of rows affected by remote UPDATE, INSERT or DELETE statement
How to run FoxPro application under different user name (impersonating user)
How to view system icons for the classes installed on the local machine
Retrieving the state of your Internet connection
Using Beep and Sleep functions to make the old tin buzz sing (WinNT only?)
Building a tree of subdirectories for a given path using FindFile functions

User rating: 9/10 (2 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 code stores into a cursor path and size data for all subdirectories within a specified path.

 
DO declare
 
LOCAL loDir
loDir = CreateObject("Tdir", "C:\program files")
GO TOP
BROWSE NORMAL NOWAIT
 
DEFINE CLASS Tdir As Custom
cursorname=""
treelevel=0
root=""
recordid=0
parentid=-1
maxid=0
 
PROCEDURE Init(lcPath, loParent)
    IF TYPE("loParent")="O"
        THIS.cursorname = loParent.cursorname
        THIS.root = loParent.root
        THIS.root.maxid = THIS.root.maxid + 1
        THIS.recordid = THIS.root.maxid
        THIS.parentid = loParent.recordid
        THIS.treelevel = loParent.treelevel + 1
    ELSE
        THIS.root = THIS
        THIS.cursorname = "cs" + SUBSTR(SYS(2015), 3,10)
 
        SELECT 0
        CREATE CURSOR (THIS.cursorname) (recordid N(6), parentid N(6),;
            treelevel N(3), dircount N(6), filecount N(6),;
            filesize N(12), utclastwrite T, fullname C(250))
    ENDIF
 
    INSERT INTO (THIS.cursorname) VALUES (THIS.recordid,;
        THIS.parentid, THIS.treelevel, 0,0,0, {}, lcPath)
 
    THIS.DoFind(lcPath)
 
PROCEDURE DoFind(lcPath)
#DEFINE MAX_PATH 260
#DEFINE FILE_ATTRIBUTE_DIRECTORY 16
#DEFINE INVALID_HANDLE_VALUE -1
#DEFINE MAX_DWORD 0xffffffff+1
 
*| typedef struct _WIN32_FIND_DATA {
*|   DWORD    dwFileAttributes;           0:4
*|   FILETIME ftCreationTime;             4:8
*|   FILETIME ftLastAccessTime;          12:8
*|   FILETIME ftLastWriteTime;           20:8
*|   DWORD    nFileSizeHigh;             28:4
*|   DWORD    nFileSizeLow;              32:4
*|   DWORD    dwReserved0;               36:4
*|   DWORD    dwReserved1;               40:4
*|   TCHAR    cFileName[ MAX_PATH ];     44:260
*|   TCHAR    cAlternateFileName[ 14 ]; 304:14
*| } WIN32_FIND_DATA, *PWIN32_FIND_DATA; total bytes = 318
#DEFINE FIND_DATA_SIZE  318
 
    lcPath = ALLTRIM(lcPath)
    IF Right(lcPath,1) <> "\"
        lcPath = lcPath + "\"
    ENDIF
 
    LOCAL hFind, cFindBuffer, lnAttr, cFilename, nFileCount,;
        nDirCount, nFileSize, cWriteTime, nLatestWriteTime, oNext
 
    cFindBuffer = Repli(Chr(0), FIND_DATA_SIZE)
    hFind = FindFirstFile(lcPath + "*.*", @cFindBuffer)
    IF hFind = INVALID_HANDLE_VALUE
        RETURN
    ENDIF
 
    STORE 0 TO nDirCount, nFileCount, nFileSize, nLatestWriteTime
    DO WHILE .T.
        lnAttr = buf2dword(SUBSTR(cFindBuffer, 1,4))
        cFilename = SUBSTR(cFindBuffer, 45,MAX_PATH)
        cFilename = Left(cFilename, AT(Chr(0),cFilename)-1)
 
        cWriteTime = SUBSTR(cFindBuffer, 21,8)
        IF EMPTY(nLatestWriteTime)
            nLatestWriteTime = cWriteTime
        ELSE
            IF CompareFileTime(cWriteTime, nLatestWriteTime) = 1
                nLatestWriteTime = cWriteTime
            ENDIF
        ENDIF
 
        IF BITAND(lnAttr, FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY
        * for a directory
            nDirCount = nDirCount + 1
            IF Not LEFT(cFilename,1)="."
                oNext = CreateObject("Tdir", lcPath + cFilename + "\", THIS)
            ENDIF
        ELSE
        * for a regular file
            nFileCount = nFileCount + 1
            nFileSize = nFileSize +;
                buf2dword(SUBSTR(cFindBuffer, 29,4)) * MAX_DWORD +;
                buf2dword(SUBSTR(cFindBuffer, 33,4))
        ENDIF
 
        IF FindNextFile(hFind, @cFindBuffer) = 0
            UPDATE (THIS.cursorname) SET dircount = m.nDirCount,;
                filecount = m.nFileCount, filesize = m.nFileSize,;
                utclastwrite = sys2dt(nLatestWriteTime);
                WHERE recordid = THIS.recordid
            EXIT
        ENDIF
    ENDDO
    = FindClose(hFind)
ENDDEFINE
 
FUNCTION sys2dt(lcFiletime)
* converts a SYSTEMTIME buffer to the DATETIME value
    LOCAL lcSystime, lcStoredSet, lcDate, lcTime, ltResult,;
        lnYear, lnMonth, lnDay, lnHours, lnMinutes, lnSeconds
 
    lcSystime = Repli(Chr(0), 16)
    = FileTimeToSystemTime(lcFiletime, @lcSystime)
 
    lnYear = buf2word(SUBSTR(lcSystime, 1,2))
    lnMonth = buf2word(SUBSTR(lcSystime, 3,2))
    lnDay = buf2word(SUBSTR(lcSystime, 7,2))
    lnHours = buf2word(SUBSTR(lcSystime, 9,2))
    lnMinutes = buf2word(SUBSTR(lcSystime, 11,2))
    lnSeconds = buf2word(SUBSTR(lcSystime, 13,2))
 
    lcStoredSet = SET("DATE")  
    SET DATE TO MDY  
 
    lcDate = STRTRAN(STR(lnMonth,2) + "/" + STR(lnDay,2) +; 
        "/" + STR(lnYear,4), " ","0")  
 
    lcTime = STRTRAN(STR(lnHours,2) + ":" + STR(lnMinutes,2) +; 
        ":" + STR(lnSeconds,2), " ","0")  
 
    ltResult = CTOT(lcDate + " " + lcTime)  
    SET DATE TO &lcStoredSet  
RETURN  ltResult
 
FUNCTION buf2word(lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) +;
    Asc(SUBSTR(lcBuffer, 2,1)) * 256
 
FUNCTION buf2dword(lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) +;
    Asc(SUBSTR(lcBuffer, 2,1)) * 256 +;
    Asc(SUBSTR(lcBuffer, 3,1)) * 65536 +;
    Asc(SUBSTR(lcBuffer, 4,1)) * 16777216
 
PROCEDURE declare
    DECLARE INTEGER FindClose IN kernel32 INTEGER hFindFile
 
    DECLARE INTEGER FindFirstFile IN kernel32;
        STRING lpFileName, STRING @lpFindFileData
 
    DECLARE INTEGER FindNextFile IN kernel32;
        INTEGER hFindFile, STRING @lpFindFileData
 
    DECLARE INTEGER FileTimeToSystemTime IN kernel32;
        STRING lpFileTime, STRING @lpSystemTime
 
    DECLARE INTEGER CompareFileTime IN kernel32;
        STRING lpFileTime1, STRING lpFileTime2
 
 
 

User rating: 9/10 (2 votes)
Rate this code sample:
  • ~
4871 bytes  
Created: 2001-12-28 18:46:58  
Modified: 2011-12-10 09:20:22  
Visits in 7 days: 54  
Listed functions:
CompareFileTime
FileTimeToSystemTime
FindClose
FindFirstFile
FindNextFile
Printer friendly API declarations
My comment:
The last write time retrieved in this code is the UTC value, so it can be different from the local time on your computer. Use the GetTimeZoneInformation to get the constant difference in minutes between both times.

The most but not all the data returned in WIN32_FIND_DATA structures can also be retrieved by calling ADIR() function.

* * *
Also IShellFolder::EnumObjects can be used to enumerate file system folders, as well as other types of folders and objects in the system -- for example, special folders, printers, subfolders inside a compressed (zipped) folder.
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:
Bob | 2005-07-23 23:01:05
Hi,

I tried this code and it does not generate folders that have periods in them

bobstranks@telus.net

thx
A.M. | 2005-07-24 06:43:41
Hi Bob,

I fixed the code, it's working now as suggested.
tsoda | 2007-02-07 12:21:21
Is there an example for finding the location of a specific filename from within a container of many folders?
A.M. | 2007-02-07 13:11:54
There's no such example, unfortunately, in this reference. As far as I know, NTFS and FAT systems do not support a single location or object that stores names of all files and folders.

http://en.wikipedia.org/wiki/WinFS

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 (54.234.126.92)
32.52 min.Function: 'SetFilePointer'
Function group: 'File Management'
32.57 min.Example: 'Using EnumPrinters function to enumerate locally installed printers'
32.63 min.Function: 'MapVirtualKey'
Function group: 'Keyboard Input'
39.35 min.Example: 'Enumerating the subkeys of a user-specific key'
3.61 hrs.Example: 'Winsock: sending email messages (SMTP, port 25)'
5.23 hrs.Function: 'CeRegQueryInfoKey'
6.52 hrs.Example: 'How to view icons stored in executable files (Icon Viewer) - II'
 Example: 'How to retrieve adapter information for the local computer (including MAC address)'
10.11 hrs.Function: 'OpenPrinter'
Function group: 'Printing and Print Spooler'
 Example: 'How to find which fonts Windows uses for drawing captions, menus and message boxes'
Google
Advertise here!