Ada Programming/Libraries/Ada.Directories

From Wikibooks, open books for an open world
Jump to navigation Jump to search


This language feature is only available from Ada 2005 on.

The package Ada.Directories allows various manipulations and queries of the filesystem directories.

The Package Directories[edit]

Used for simple directory manipulation as defined in Ada 2005.

Traversing Directories[edit]

Changing the current working directory is done with Set_Directory. The remaining functions are fairly self-explanatory with the exception of Containing_Directory, which returns the name of the directory containing the given directory.

    function Current_Directory return String;
    procedure Set_Directory (Directory : in String);
    function Exists (Name : in String) return Boolean;
    function Containing_Directory (Name : in String) return String;

Enumerating Directory Entries[edit]

This example enumerates the all the entries in the current working directory, that end in ".gpr". The Start_Search procedure is used to start enumerating a directory. It will To remove the filter criteria you can pass an empty string. Iterating through the directory involves calling Get_Next_Entry to return the next directory entry and More_Entries to check for more entries. Once the search is complete, call End_Search.

    type Search_Type is limited private;
    type Directory_Entry_Type is limited private;

    procedure Start_Search (Search    : in out Search_Type;
                           Directory : in String;
                           Pattern   : in String;
                           Filter    : in Filter_Type := (others => True));
    procedure Get_Next_Entry (Search : in out Search_Type; Directory_Entry : out Directory_Entry_Type);
    function More_Entries (Search : in Search_Type) return Boolean;
    procedure End_Search (Search : in out Search_Type);

The resulting Directory_Entry_Type has a full name, simple name, size, kind, and modification type.

   function Simple_Name (Directory_Entry : in Directory_Entry_Type) return String;
   function Full_Name (Directory_Entry : in Directory_Entry_Type) return String;
   function Kind (Directory_Entry : in Directory_Entry_Type) return File_Kind; -- Directory, Ordinary_File, Special_File
   function Size (Directory_Entry : in Directory_Entry_Type) return File_Size;
   function Modification_Time (Directory_Entry : in Directory_Entry_Type) return Ada.Calendar.Time;
with Ada.Text_IO;
with Ada.Directories;
use Ada.Text_IO;
use Ada.Directories;
procedure Main is
   Dir : Directory_Entry_Type;
   Dir_Search : Search_Type;
   Curr_Dir : string := Current_Directory;
   Put("Current Directory: ");
   Start_Search(Search => Dir_Search,
                Directory => Curr_Dir,
                Pattern => "*.gpr");
      Get_Next_Entry(Dir_Search, Dir);
      if Kind(Dir) = Ordinary_File then
      end if;
      exit when not More_Entries(Dir_Search);
   end loop;
end Main;

Directory Manipulation[edit]

Creating a directory is done with Create_Directory. Create_Path creates all the directories in the path. The directory path on GNAT Ada can contain either "\" or "/" characters. Rename renames a directory or file within a directory. <Delete_Tree> deletes an entire hierarchy of directories and files. Delete_Directory deletes an empty directory (non-empty directories throw a Use_Error exception. Delete_File deletes an ordinary file in a directory. Note that the Form parameters is implementation specific. On the version of GNAT Ada I'm using, it can only be used to set the encoding.

    procedure Create_Directory (New_Directory : in String; Form : in String := "");
    procedure Create_Path (New_Directory : in String; Form : in String := "");
    procedure Rename (Old_Name, New_Name : in String);

    procedure Delete_Tree (Directory : in String);
    procedure Delete_Directory (Directory : in String);
    procedure Delete_File (Name : in String);

Below is a simple example that creates a set of directories and iterates through those directories. Note that it runs on both Windows and Linux with the "/" path separator. The returned path separators are appropriate to the filesystem (e.g. Windows return "\").

procedure Main is
   Dir : Directory_Entry_Type;
   Dir_Search : Search_Type;   
   Start_Search(Search => Dir_Search, Directory => Curr_Dir, Pattern => "");
      Get_Next_Entry(Dir_Search, Dir);
      exit when not More_Entries(Dir_Search);
   end loop;
end Main;


Status_Error is raised if a directory entry is invalid or when enumerating directories past the last directory. Name_Error is usually thrown if the directory or filename is not able to identify a file or directory (either non-existent or invalid depending on context). Use_Error is thrown if directories are not support or directories are not traversable.

   Status_Error : exception renames Ada.IO_Exceptions.Status_Error;
   Name_Error   : exception renames Ada.IO_Exceptions.Name_Error;
   Use_Error    : exception renames Ada.IO_Exceptions.Use_Error;
   Device_Error : exception renames Ada.IO_Exceptions.Device_Error;

See Also[edit]

External examples[edit]

Ada 95 Reference Manual[edit]

Ada 2005 Reference Manual[edit]

Ada Quality and Style Guide[edit]