Open main menu

Ada Programming/Libraries/Ada.Directories

< Ada Programming‎ | Libraries

The Package DirectoriesEdit

Used for simple directory manipulation as defined in Ada 2005.

Traversing DirectoriesEdit

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 EntriesEdit

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;
begin
   Put("Current Directory: ");
   Put_Line(Curr_Dir);
    
   Start_Search(Search => Dir_Search,
                Directory => Curr_Dir,
                Pattern => "*.gpr");
   loop
      Get_Next_Entry(Dir_Search, Dir);
      
      Put(Full_Name(Dir));
      Set_Col(50);
      
      if Kind(Dir) = Ordinary_File then
         Put(Size(Dir)'Image);
      end if;
      Set_Col(60);
      
     Put_Line(Kind(Dir)'Image);
     
      exit when not More_Entries(Dir_Search);
   end loop;
   
   End_Search(Dir_Search);
   
end Main;

Directory ManipulationEdit

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;   
begin   
   Start_Search(Search => Dir_Search, Directory => Curr_Dir, Pattern => "");
   
   Create_Path("Foo");
   Create_Path("Bar/Baz/Qux");
   
   loop
      Get_Next_Entry(Dir_Search, Dir);
      Put_Line(Full_Name(Dir));
      exit when not More_Entries(Dir_Search);
   end loop;
   
   End_Search(Dir_Search);
   
   Delete_Directory("Foo");
   Delete_Tree("Bar");
   
end Main;

ExceptionsEdit

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 AlsoEdit

External examplesEdit

Ada 95 Reference ManualEdit

Ada 2005 Reference ManualEdit

Ada Quality and Style GuideEdit