Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use Windows API to find file

API Functions

Use Windows API to find file

by  danceman  Posted    (Edited  )
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top