INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

API Functions

Use Windows API to find file by danceman
Posted: 19 Mar 05

I use this to find if a file exist on a users system at the time of running setup.exe.  If the file exist I can then take other action.  This example only searches for the file once, if found, returns the path.

PARAMETERS lcPath, lcfile
** find file located in the path passed or subfolder
IF PARAMETERS() < 1
    RETURN -1
ENDIF
IF EMPTY(lcPath)
    lcPath = "C:\"  
ENDIF
IF EMPTY(lcfile)
    RETURN -1
ENDIF
LOCAL hFind, lcFindBuffer, lnAttr, lcFilename, lcFindFileBuffer, hFindFile

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
#DEFINE FILE_ATTRIBUTE_DIRECTORY  16
#DEFINE MAX_PATH                 260
#DEFINE INVALID_HANDLE_VALUE      -1
#DEFINE MAX_DWORD         4294967296  && 0xffffffff + 1
#DEFINE FIND_DATA_SIZE  318

*| 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

lcPath = ALLTRIM(lcPath)
IF RIGHT(lcPath,1) <> "\"
    lcPath = lcPath + "\"
ENDIF

** buffer used to search for the file in each folder identified
lcFindFileBuffer = REPLI(CHR(0), FIND_DATA_SIZE)
** buffer used to start search of path passed
lcFindBuffer = REPLI(CHR(0), FIND_DATA_SIZE)

hFind = FindFirstFile (lcPath + "*.*", @lcFindBuffer)
IF hFind = INVALID_HANDLE_VALUE
    RETURN -1
ENDIF

DO WHILE .T.
    lnAttr = buf2dword(SUBSTR(lcFindBuffer, 1,4))
    lcFilename = SUBSTR(lcFindBuffer, 45,MAX_PATH)
    lcFilename = LEFT(lcFilename, AT(CHR(0),lcFilename)-1)

    ** test for directory
    IF BITAND(lnAttr, FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY
        * for a directory
        IF NOT "." $ lcFilename
            ** look for file in ths folder
            hFindFile = FindFirstFile (lcPath + lcFilename + "\" + lcfile, @lcFindFileBuffer)
            IF hFindFile != INVALID_HANDLE_VALUE   && file located quit with the path to the file
                FindClose(hFind)
                FindClose(hFindFile)
                RETURN lcPath + lcFilename + "\"
            ENDIF
        ENDIF
    ENDIF
    IF FindNextFile (hFind, @lcFindBuffer) = 0
        EXIT
    ENDIF
ENDDO
FindClose(hFind)
RETURN .f.

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

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close