I was navigating through your archives looking for a "File Search" utility, to avoid CMD and spell everything out.
Eurika! I found something. It was done by blindpete@mail.com. It gave me some problems with locked directories and I had to go and wreck the locks on every directory which had a lock on it. Finally it went all the way through the search, but the result, thought correct, in the class before the RETURN, when it returns it delivers a .T. instead of something like "C:\Flakey\PRGs\start.prg". I fought with it at length and finally had to give up and force transfer the result through a public variable.
This is the call to the Class Object and you can find the return near the end of the code with my comments around it and what I had to do to get it to work.
Maybe someone has an idea why this is taking place...
And this is the Behemoth Class Object wrapped in my function fondfile:
Eurika! I found something. It was done by blindpete@mail.com. It gave me some problems with locked directories and I had to go and wreck the locks on every directory which had a lock on it. Finally it went all the way through the search, but the result, thought correct, in the class before the RETURN, when it returns it delivers a .T. instead of something like "C:\Flakey\PRGs\start.prg". I fought with it at length and finally had to give up and force transfer the result through a public variable.
This is the call to the Class Object and you can find the return near the end of the code with my comments around it and what I had to do to get it to work.
Maybe someone has an idea why this is taking place...
Code:
LPARAMETERS nKeyCode, nShiftAltCtrl
IF nKeyCode = 13
ThisForm.srchScreen.ff_result.Value = FindFile(ALLTRIM(This.Value))do init_vfp
ENDIF
And this is the Behemoth Class Object wrapped in my function fondfile:
Code:
FUNCTION findfile
LPARAMETERS cfilename
cfilename = ALLTRIM(cfilename)
*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])
[COLOR=#CC0000]ofind = CREATEOBJECT("FileSearch")[/color]
*This example returns a string
[COLOR=#CC0000]RETURN ofind.search(cfilename,cfilename+" - ",.T.,.T.,.T.)[/color]
*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
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
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()
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"
cbartext = ""
ENDIF
IF TYPE("bshow") <> "L"
bshow = .F.
ENDIF
IF TYPE("bMore") <> "L"
bmore = .F.
ENDIF
IF TYPE("bCmdButtons") <> "L"
bcmdbuttons = .T.
ENDIF
IF AT("?",cfilename)>0 OR AT("*",cfilename)>0
THISFORM.wildcard = .T.
ENDIF
*Init Display
IF bshow
THISFORM.counter = 0
THISFORM.VISIBLE = .T.
THISFORM.otxtmore.VISIBLE = bmore
THISFORM.ocmd1.VISIBLE = bcmdbuttons
THISFORM.ocmd1.ENABLED = bcmdbuttons
THISFORM.barcaption = cbartext
IF bmore
THISFORM.HEIGHT = 77
THISFORM.otxtmore.TOP = THISFORM.HEIGHT -31
ELSE
THISFORM.HEIGHT = 46
ENDIF
IF bcmdbuttons
THISFORM.HEIGHT = THISFORM.HEIGHT + 31
THISFORM.ocmd1.TOP = THISFORM.HEIGHT -31
THISFORM.ocmd1.SETFOCUS
ENDIF
ENDIF
*Directory List
IF THISFORM.wildcard
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 &&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.
bkeepgoing =.F.
cdrivepathfile = ""
ndrive = 91 && end drive search FOR LOOP
EXIT && end current drive DO LOOP
ENDIF
*Look for file
IF THISFORM.wildcard
SELECT filelist
ADIR(afilelist,"&cPath.&cFileName","AHRS") && copy mathching filenames into an array
cpathnow = SYS(5)+CURDIR()
IF TYPE("aFileList") <> "U"
cfoldername = "&cPath"
FOR ncopy = 1 TO ALEN(afilelist,1)
IF RIGHT(afilelist[nCopy,5],1)<>"D"
APPEND BLANK
REPLACE folder WITH cfoldername
REPLACE filename WITH afilelist[nCopy,1]
ENDIF
ENDFOR
RELEASE afilelist
ENDIF
ELSE
IF FILE("&cPath.&cFileName")
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<>"."
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
cdrivepathfile = "&cPath.&cFileName"
ELSE
cdrivepathfile = ""
ENDIF
SELECT dirlist &&kill cursor
USE
IF THISFORM.wildcard
SELECT filelist && kill cursor if empty
IF RECCOUNT() = 0
USE
ENDIF
ENDIF
THISFORM.result = cdrivepathfile
THISFORM.VISIBLE = .F.
THISFORM.buttoncancel =.F.
*[COLOR=#EF2929] I introduced this line below to bypass the RETURN below. And it works just fine.[/color]
this_form.srchscreen.ff_result.VALUE = cdrivepathfile
* [COLOR=#EF2929]The RETURN below fails. It returns .T. EVERY TIME! What gives???[/color]
RETURN cdrivepathfile
ENDPROC
ENDDEFINE
*EOF
ENDPROC
RETURN