TURBO PASCAL |
Новости
|
45: How to establish if a name refers to a directory or not?A: This question has turned out a bit more complicated than I first thought. There are several methods, each with some catch. The first is trying to open the name as a file and observing the IOResult. The ISDIRFN function in ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip TPU unit TSUNTJ.TPU is based on this method. Unfortunately it is not always stable. I have been reported problems in connection with DRDOS by Richard Breuer (ricki@pool.informatik.rwth-aachen.de) who has tested these routines. The second method (ISDIR2FN) is based on the fact that the file NUL exists in a directory if the directory exists. The thrid method (ISDIR3FN) is a brute force method. It is given below, since it is quite an instructive little exercise of Turbo Pascal programming. (* Search recursively through a drive's directories. Auxiliary, recursive procedure for ISDIR3FN *) procedure SEARCHDR (Path, FileSpec : string; name : string; var found : boolean); var FileInfo : SearchRec; begin FindFirst (Path + '*.*', Directory, FileInfo); while DosError = 0 do begin if ((FileInfo.Attr and Directory) > 0) and (FileInfo.Name <> '.') and (FileInfo.Name <> '..') then begin SEARCHDR (Path + FileInfo.Name + '\', FileSpec, name, found); if Path + FileInfo.Name + '\' = name then found := true; end; FindNext (FileInfo); end; {while} end; (* searchdr *) (* Does a name refer to a directory *) function ISDIR3FN (name : string) : boolean; var drive : char; found : boolean; begin {... Default value ...} isdir3fn := false; {... Discard empty names ...} if name = '' then exit; {... Expand into a fully qualified name, makes it uppercase ...} name := FExpand (name); if name[Length(name)] <> '\' then name := name + '\'; {... Extract the drive letter from the name ...} drive := UpCase (name[1]); {... Check first for the root ...} if drive + ':\' = name then begin isdir3fn := true; exit; end; {... Check the rest of the directories recursively ...} found := false; SEARCHDR (drive + ':\', '*.*', name, found); isdir3fn := found; end; (* isdir3fn *) -Date: Mon, 13 Jun 1994 00:13:05 +0000 (GMT) -From: JEROEN SCHIPPER <JSCHIPPER@HUT.NL> -To: ts@uwasa.fi (Timo Salmi) -Subject: Is a name a directory in TP The method I use is simply checking the attribute bit, as this small program will demonstrate: program isdir; uses dos; var s:string; attr:word; f:file; begin repeat readln(s); if s = '' then break; assign(f,s); getfattr(f,attr); if doserror <> 0 then writeln('DOS error code = ', doserror) else begin if attr and directory <> 0 then writeln(S,' is a directory') else writeln(S,' is a not directory') end; until false; end. The methods you mention in your faq are far more complicated, but why? Is there are catch why the method above won't work? I guess don't really understand the problem here. Jeroen. A2: This has turned out to be a tricky FAQ. There are some additional suggestions and comments from the gentle readers. You can track them from ftp://garbo.uwasa.fi/pc/ts/tspost00.zip index. |
(с)Все права защищены По всем интересующим вопросам прошу писать на электронный адрес |