TURBO PASCAL

Новости

Программы   

Turbo Pascal 

Игры

Документация   

Странности

FAQ

Ссылки

Форум

Гостевая книга

Рассылка

Благодарности

Об авторе

47: How can I test whether a file exists?

 A: There are several alternatives. Here is the most common with
example code. It recognizes also read-only, hidden and system files.
  function FILEXIST (name : string) : boolean;
  var fm : byte;
      f  : file;
      b  : boolean;
  begin
    fm := FileMode;
    FileMode := 0;
    assign (f, name);
    {$I-} reset(f); {$I+}
    b := IOResult = 0;
    if b then close(f);
    filexist := b;
    FileMode := fm;
  end;
A second alternative is
  Uses Dos;
  function FILEXIST (name : string) : boolean;
  var f  : file;
      a  : word;
  begin
    assign (f, name);
    GetFAttr (f, a);
    filexist := false;
    if DosError = 0 then
      if ((a and Directory) = 0) and ((a and VolumeId) = 0) then
        filexist := true;
  end;
A third alternative is
  Uses Dos;
  function FILEXIST (name : PathStr) : boolean;
  begin
    filexist := FSearch (name, '') <> '';
  end;
A fourth alternative is the following. Be careful with this option,
since it works a bit differently from the others. It accepts wild
cards. Thus, for example FILEXIST('c:\autoexec.*') would be TRUE in
this method, while FALSE in all the above.
  Uses Dos;
  function FILEXIST (name : string) : boolean;
  var f : SearchRec;
  begin
    filexist := false;
    FindFirst (name, AnyFile, f);
    if DosError = 0 then
      if (f.attr <> Directory) and (f.attr <> VolumeId) then
        filexist := true;
  end;
A good variation from KDT@newton.national-physical-lab.co.uk of this
theme, disallowing wildcards:
  function file_exists (fname :string) :boolean;
  var f :searchrec;
  begin
    findfirst (fname, anyfile - directory - volumeid, f);
    file_exists := (doserror + pos('*',fname) + pos('?',fname) = 0);
  end;

 Contents

 

На первую страницу

Rambler's Top100 Rambler's Top100
PROext: Top 1000

(с)Все права защищены

По всем интересующим вопросам прошу писать на электронный адрес

Hosted by uCoz