comp.lang.ada
 help / color / mirror / Atom feed
* Re: How to know whether a file exists or not?
@ 1996-08-24  0:00 John Herro
  0 siblings, 0 replies; only message in thread
From: John Herro @ 1996-08-24  0:00 UTC (permalink / raw)



Markus Wahl <d95wahl@dtek.chalmers.se> writes:
> How can I make my program find out
> which files that exist in a certain directory?
     To find out if a certain file exists, you can use this function:

with Ada.Text_IO; use Ada.Text_IO;
function Exists(File_Name : in String) return Boolean is
   F      : File_Type;
   Answer : Boolean := True;
begin
   begin
      Open(F, In_File, File_Name);
      Close(F);
   exception
      when Name_Error => Answer := False;
   end;
   return Answer;
end Exists;

     If you are using Ada 83 instead of Ada 95, leave out the "Ada." in
two places in the first line of this function.
     Unfortunately, the designers of Ada 95 didn't add a function to
Text_IO to check if a file exists.  The only way I know how to check if a
file exists is to attempt to open the file and trap the error if the open
fails.
     This function assumes that the file name represents a text file, but,
at least on my system, it works with any type of file, because it only
opens and closes the file.
     If you want a list of ALL the files in a certain directory, there are
two things you can do.  One is to shell to the operating system and send
the output of a "directory" command to a file, which your program can then
open and read.
     The other is to use the appropriate operating system calls, if your
Ada compiler supports them.  For example, Open Ada for DOS has a package
File_IO which supports calls to the DOS functions "Find First" and "Find
Next."  You can use these to create a list of all the files in a
directory.  I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1996-08-24  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-24  0:00 How to know whether a file exists or not? John Herro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox