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)
Detecting changes in connections to removable drives (VFP9)
How to download a file from the FTP server using FtpGetFile
Using Font and Text functions
Using Video Capture: displaying on FoxPro form frames and previewing video obtained from a digital camera
How to play AVI file on the _screen
Using EnumPrinters function to enumerate locally installed printers
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
Splash Screen for the VFP application
Subclassing CommandButton control to create BackColor property
Vertical Label control
Enumerating network resources
Creating a console window for Visual FoxPro application
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 (50.17.109.248)
6 sec.Function: 'CeEnumDBVolumes'
7.73 hrs.Function: 'LoadLibrary'
 Example: 'Validating the heap of the calling process'
8.77 hrs.Example: 'Form Magnifier'
 Function: 'DllInstall'
 Example: 'GetFileOwner - Get the owner of an NTFS file'
10.4 hrs.Function: 'mixerGetNumDevs'
Function group: 'Windows Multimedia'
 Example: 'Using GetSysColor'
10.63 hrs.Example: 'Using the RestartDialog function -- restarting Windows'
 Example: 'Using the heap of the calling process to allocate memory blocks'
Google
Advertise here!