for the record, and for all those interested,
---------- Forwarded message ---------- From: Luis Rivera jlrn77@gmail.com Date: 22 May 2010 14:56 Subject: Re: findfiles syntax To: Maurice Lombardi
On 22 May 2010 03:19, Maurice Lombardi Maurice.Lombardi@ujf-grenoble.fr wrote:
Luis Rivera a écrit :
program FindFilesTest;
uses GPC, FileUtils;
type alphafile=text; var nameoffile: string; filename: alphafile; const mydir='/usr/local/';
function aopenin(var a:alphafile):boolean; begin if ioresult=0 then assign(a,trim(nameoffile)); findfiles(mydir,a,false,reset,nil); aopenin:=ioresult=0; end;
begin WriteLn ('Enter file name to type: '); ReadLn (nameoffile); if aopenin(alphafile) then
[...] Declaring the text file as filename is very confusing: this is certainly the source of your errors: you confuse a file and its name.
- a is a file, not a string (Mask)
mydir is a string (the name of the directory to search in)
- reset has for parameter a file, not a string.
So it expects the string containing the filename...
Changing the naming conventions, trying to avoid confusing the FileName (string) with the FileContents (text), I may do the following:
program AtBat;
uses GPC, FileUtils;
type alphafile=text; var filename: string; filecontents: alphafile; const mydir='/usr/local/';
function aopenin(var a:alphafile):boolean; var f:string; begin if ioresult=0 then f:=trim(filename); assign(a,f); findfiles(mydir,f,false,reset,nil); aopenin:=ioresult=0; end;
begin WriteLn ('Enter file name to type: '); ReadLn (filename); if aopenin(filecontents) then WriteLn(filename, ' found!') else Writeln('Sorry: search failed!'); end.
However, I still get the same error message...
BTW: a simple, sensible way to state "DoNothing" as DirAction?
You might want to declare a FileAction procedure with a string parameter (nameoffile): probably you would include in it the assign which is before findfiles in your program and the reset
That's what I just tried to do! :-)
Notice that if there are multiple files found, you will have an error because you then try to reopen a file (a) which has not been closed: findfiles apply FileAction on all files which match Mask.
Maybe I can get around that problem defining the FileAction procedure in such a way that wildcards are forbidden...
You can close the file inside FileAction, but beware not to confuse nameoffile as a parameter of FileAction and as a global variable: if you give two different names and assign the global name to the parameter inside FileAction you can recover at the end the last nameoffile found.
PS:
A := B = C; is correct in Pascal, weird but correct.
Cool. Thanks for pointing that out.
Cheers!
-- Luis Rivera O< http://www.asciiribbon.org/ campaign