Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!hp4nl!eurtrx!euraiv1!reino From: reino@cs.eur.nl (Reino de Boer) Newsgroups: comp.lang.pascal Subject: Re: Find File? Message-ID: <1991Feb15.112843.13779@cs.eur.nl> Date: 15 Feb 91 11:28:43 GMT References: <1129@tesla.njit.edu> <91042.205829SPRAGGEJ@QUCDN.QueensU.CA> Reply-To: reino@cs.eur.nl Organization: Erasmus University Rotterdam Lines: 47 In <91042.205829SPRAGGEJ@QUCDN.QueensU.CA> SPRAGGEJ@QUCDN.QueensU.CA (John G. Spragge) writes: >Regarding a question about how to check and see if a file exists: >There are two methods. The first is to try and open the file with >Pascal run-time I/O error halting turned off. This is set by the >$I switch; thus, if you include {$I-} in your file, an attempt >to open the file will not halt the program. To see whether the file >was opened successfully, use the IORESULT function. Assuming you mean something like function exists( filename : pathstr ) : boolean; var f : file; begin assign( f, filename ); {$I-} reset( f ); {$I+} if ioresult = 0 then begin { file exists } close( f ); exists := true end else { file doesn't exist } exists := false end; This method can fail for two reasons: 1. in old versions of Turbo Pascal exists( 'con' ) gives an incorrect result, because it's explicitly of type text. But, more importantly: 2. the file is not found if it's a read-only file on a network drive. This is because untyped files (and any other non-text files) are opened in update mode by reset( .. ). >The second method is to use the FINDFIRST command. And be sure to check whether it is a volume-label, directory, or file. Hope this helps -- Reino -- Reino R. A. de Boer "We want to build the right product right, right?" Erasmus University Rotterdam ( Informatica ) e-mail: reino@cs.eur.nl