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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search for files with VFP w/o FILER.DLL ?

Tips -N- Tricks

How to search for files with VFP w/o FILER.DLL ?

by  BlindPete  Posted    (Edited  )
*FileSearch Class
* 7/2000
* blindpete@mail.com

* DESCRIPTION
* This Class will search all drives from C to Z (that exist)
* for the filename that you pass it. It returns a string
* [Drive]:[Path][Filename w/ extension] and empty() if not
* found. If wildcards (*/?) are used it returns a cursor.

* USAGE
* cPathAndFilename = oFileSearch.Search(cFileName)
* ObjectName.Search(cFileName,[cBarText],[bShowWindow],[bMoreInfo],[bCmdButtons])
* cFileName REQD character filename and extension Wildcards (*?) allowed
* [cBarText] OPTN character context to place in progress bar Default empty()
* [bShowWindow] OPTN T/F display form or not Default .F.
* [bMoreInfo] OPTN T/F display path being searched Default .F.
* [bCmdButtons] OPTN T/F display CANCEL button or not Default .F.

* RETURNS
* IF Found: [Drive]:[Path][Filename w/ extension]
* w/ wildcards cursor FILELIST (FOLDER C(254), FILENAME C(254)
* IF Not Found: empty()
* w/ wildcards cursor FILELIST (FOLDER C(254), FILENAME C(254)

* BEGGING
* Should you make any significant improvements to this
* class please send it back to me blindpete@mail.com

* DISCLAIMER
* This Class is as is. There is no warrenty expressed or
* implied. Use this code at your own risk.

* CREDITS
* Much thanks to Chris Chamberlain and a big thank you to
* Jon Scott who enlightened me on the beauty and hazards
* of the DOEVENTS command.

*EXAMPLE/SAMPLE(s)
*oFind.Search(cFileName,[cBarText],[bShowWindow],[bMoreInfo],[bCmdButtons])
oFind = createobject("FileSearch")

*This example returns a string
? oFind.Search("Config.sys","Config.sys - ",.T.,.T.,.T.)
*This example creates a cursor
? oFind.Search("*.sys","All System Files - ",.T.,.T.,.T.)

**********************************************
DEFINE CLASS "FileSearch" AS "FORM"
COUNTER = 0
Result = ""
BarCaption = ""
ButtonCancel =.F.
WildCard = .F.
ALWAYSONTOP = .T.
AUTOCENTER = .T.
SHOWWINDOW = 2 && top level form
WINDOWTYPE = 1 && Modal
DRAWMODE = 9
TITLEBAR = 1
CONTROLBOX = .F.
HEIGHT = 83
WIDTH = 264+24
CAPTION = "File Search..."
ADD OBJECT "oTxtMore" AS "TEXTBOX" WITH ;
HEIGHT = 25, LEFT =12, WIDTH = 264, ALIGNMENT = 0, ;
TOP = 76, BACKSTYLE = 0, BORDERSTYLE = 0, FONTBOLD = .F.,;
TABSTOP = .F., ENABLED=.F., DISABLEDFORECOLOR = RGB(0,0,0),;
FONTSIZE = 8
ADD OBJECT "oTxt" AS "TEXTBOX" WITH ;
HEIGHT = 25, LEFT =12, WIDTH = 264, ALIGNMENT = 2, ;
TOP = 6, BACKCOLOR = RGB(255,255,255), ;
DISABLEDBACKCOLOR = RGB(255,255,255), ;
DISABLEDFORECOLOR = RGB(0,0,0), ;
ENABLED = .F., FONTBOLD = .T.
ADD OBJECT "oCmd1" AS "COMMANDBUTTON" WITH ;
CAPTION = "\<Cancel", HEIGHT = 25, LEFT = 96, ;
WIDTH = 97, TOP = 48
PROCEDURE oCmd1.CLICK
nQuit = MESSAGEBOX('Are you sure you want to cancel?',4+32,thisform.caption)
If nQuit = 6 THEN
thisform.ButtonCancel =.T. &&abort search
ENDIF
ENDPROC
ADD OBJECT "oShp" AS "SHAPE" WITH ;
BORDERSTYLE = 0, DRAWMODE = 14, FILLSTYLE = 0
PROCEDURE INIT
thisform.oshp.left=thisform.oTxt.left+1
thisform.oshp.top=thisform.oTxt.top +1
thisform.oshp.height=thisform.oTxt.height-2
thisform.oshp.visible = .T.
thisform.borderstyle = 2
ENDPROC
PROCEDURE Counter_assign
LPARAMETERS vNewVal
IF vNewVal <> thisform.counter THEN
thisform.counter = vNewVal
x = thisform.counter
thisform.oShp.width = x*thisform.oTxt.width/100
thisform.oTxt.value = thisform.BarCaption+ALLTRIM(STR(x))+"% Complete"
thisform.refresh
*Check if cancel button has been pushed Alt+C or clicked
IF MDOWN() OR CHRSAW() THEN
DOEVENTS
ENDIF
ENDIF
ENDPROC
PROCEDURE Search
PARAMETER cFileName, cBarText, bShow, bMore, bCmdButtons
*cFileName: FileName w/ extension NO wildcards
*cBarText: Caption displayed inside the progress bar
*bShow: .T. = show progess bar
*bMore: .T. = show more information about the search progress
*bCmdButtons Displays the toolbar (cancel only at this time... more later)
*Returns: FullPath [Drive]:[Path][Filename w/ extension]
*Validate Parameters
IF TYPE("cFileName") <> "C" OR LEN(ALLTRIM(cFileName)) = 0
RETURN
ENDIF
IF TYPE("cBarText") <> "C" THEN
cBarText = ""
ENDIF
IF TYPE("bshow") <> "L" THEN
bShow = .F.
ENDIF
IF TYPE("bMore") <> "L" THEN
bMore = .F.
ENDIF
IF TYPE("bCmdButtons") <> "L" THEN
bCmdButtons = .T.
ENDIF
IF AT("?",cFileName)>0 OR AT("*",cFileName)>0 THEN
thisform.wildcard = .T.
ENDIF
*Init Display
IF bShow THEN
thisform.Counter = 0
thisform.Visible = .T.
thisform.otxtMore.Visible = bMore
thisform.oCmd1.Visible = bCmdButtons
thisform.oCmd1.Enabled = bCmdButtons
thisform.BarCaption = cBarText
IF bMore THEN
thisform.height = 77
thisform.otxtMore.top = thisform.height -31
ELSE
thisform.height = 46
ENDIF
IF bCmdButtons THEN
thisform.height = thisform.height + 31
thisform.ocmd1.top = thisform.height -31
thisform.ocmd1.setfocus
ENDIF
ENDIF
*Directory List
IF thisform.wildcard THEN
CREATE CURSOR FileList(FOLDER C(254), FileName C(254)) && For Wild Cards
ENDIF
CREATE CURSOR DirList(FOLDER M)
*CREATE CURSOR DirList(FOLDER C(254))
SELECT DirList
cStartDir = SYS(5)+CURDIR()
nRecord = 1
FOR nDrive = 65 TO 90
cDrive = CHR(nDrive)
cDir = "\"
IF DRIVETYPE(cDrive)=3 OR DRIVETYPE(cDrive)=4 THEN &&Hard or Net Drive
cPath = cDrive+":&cDir"
SET DEFAULT TO "&cPath"
APPEND BLANK
REPLACE folder WITH cPath
ADIR(aDirList,"","D") && copy all the current dirs into an array
cPathNow = SYS(5)+CURDIR()
GO nRecord
bKeepGoing =.T.
DO WHILE bKeepGoing=.T.
nRecord = RECNO()
cPath = ALLTRIM(DirList.Folder)
thisform.otxtMore.value = cPath
thisform.Counter = 100*(nRecord/RECCOUNT())
*Check if Cancel Command is in effect
IF thisform.ButtonCancel =.T. THEN
bKeepGoing =.F.
cDrivePathFile = ""
nDrive = 91 && end drive search FOR LOOP
EXIT && end current drive DO LOOP
ENDIF
*Look for file
IF thisform.wildcard THEN
SELECT FileList
ADIR(aFileList,"&cPath.&cFileName","AHRS") && copy mathching filenames into an array
cPathNow = SYS(5)+CURDIR()
IF TYPE("aFileList") <> "U" THEN
cFolderName = "&cPath"
FOR nCopy = 1 TO ALEN(aFileList,1)
IF RIGHT(aFileList[nCopy,5],1)<>"D" THEN
APPEND BLANK
REPLACE Folder WITH cFolderName
REPLACE FileName WITH aFileList[nCopy,1]
ENDIF
ENDFOR
RELEASE aFileList
ENDIF
ELSE
IF FILE("&cPath.&cFileName") THEN
nDrive = 91 &&END FOR LOOP
EXIT && END DO LOOP
ENDIF
ENDIF
*Add more directories to search
SELECT DirList
SET DEFAULT TO "&cPath"
ADIR(aDirList,"","D") && copy all the current dirs into an array
cPathNow = SYS(5)+CURDIR()
FOR nCopy = 1 TO ALEN(aDirList,1)
cFolderName = aDirList[nCopy,1]
IF RIGHT(aDirList[nCopy,5],1)="D" AND cFolderName<>"." THEN
APPEND BLANK
REPLACE Folder WITH "&cPathNow.&cFolderName.\"
ENDIF
ENDFOR
RELEASE aDirList
GO nRecord
bKeepGoing = IIF(nRecord=RECCOUNT(),.F.,.T.)
SKIP
ENDDO
ENDIF
ENDFOR
*CleanUp
SET DEFAULT to "&cStartDir" &&reset to previous default
IF bKeepGoing THEN
cDrivePathFile = "&cPath.&cFileName"
ELSE
cDrivePathFile = ""
ENDIF
SELECT DirList &&kill cursor
USE
IF thisform.wildcard THEN
SELECT FileList && kill cursor if empty
IF RECCOUNT() = 0 THEN
USE
ENDIF
ENDIF
thisform.result = cDrivePathFile
thisform.visible = .F.
thisform.ButtonCancel =.F.
RETURN cDrivePathFile
ENDPROC
ENDDEFINE
*EOF
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