ZetCode

Ada directories

last modified November 23, 2026

File names and directory names are closely related. A directory is a container for entries, each entry has a name and a kind, and a path combines a directory with a name. The package Ada.Directories provides these operations through a portable interface rather than through direct operating-system calls. This article tests whether a path exists, creates directories and whole directory trees, splits a path into its parts, reads the kind, size, and modification time of an entry, searches a directory by pattern, and removes what it created.

Testing for existence

Exists takes a path and answers whether a file or a directory of that name is present. The name "." always denotes the current directory, and a relative name is resolved against the working directory of the program. A missing name is not an error for this function: it simply returns False.

dir_exists.adb
with Ada.Text_IO;
with Ada.Directories;
use Ada.Text_IO;

procedure Dir_Exists is
begin
   Put_Line ("current dir:" & Boolean'Image
             (Ada.Directories.Exists (".")));
   Put_Line ("no_such_file.txt:" & Boolean'Image
             (Ada.Directories.Exists ("no_such_file.txt")));
end Dir_Exists;

The first line is always true, because every program runs in some directory. The second is false until someone creates a file with that name in the working directory, so it is a safe example of a missing entry. Boolean'Image prints TRUE or FALSE without a leading blank.

$ gnatmake -gnat2022 -gnatwa dir_exists.adb
$ ./dir_exists
current dir:TRUE
no_such_file.txt:FALSE

Creating directories

Create_Directory makes one new directory. It requires the parent to exist, and it raises an exception when the name is already in use, which is why the program tests Exists first. Create_Path creates every missing level of a path, so it can build demo/logs/old even when only demo was present.

dir_create.adb
with Ada.Text_IO;
with Ada.Directories;
use Ada.Text_IO;

procedure Dir_Create is
begin
   if not Ada.Directories.Exists ("demo") then
      Ada.Directories.Create_Directory ("demo");
      Put_Line ("demo created");
   end if;
   Ada.Directories.Create_Path ("demo/logs/old");
   Put_Line ("nested path exists:" & Boolean'Image
             (Ada.Directories.Exists ("demo/logs/old")));
end Dir_Create;

The guard makes the program safe to run more than once: the second run finds demo already there and prints only the nested line. Create_Path is content with an existing path, so it needs no guard. Both names are relative to the working directory, and both create directories rather than files.

$ gnatmake -gnat2022 -gnatwa dir_create.adb
$ ./dir_create
demo created
nested path exists:TRUE

The parts of a path

Four functions take a path apart without touching the file system. Simple_Name returns the last component, Containing_Directory returns everything before it, Extension returns the text after the last dot, and Base_Name returns the name without that extension. Compose does the opposite for a directory and a simple name.

dir_parts.adb
with Ada.Text_IO;
with Ada.Directories;
use Ada.Text_IO;

procedure Dir_Parts is
   Path : constant String := "demo/logs/old/report.txt";
begin
   Put_Line ("simple:" & Ada.Directories.Simple_Name (Path));
   Put_Line ("parent:" & Ada.Directories.Containing_Directory (Path));
   Put_Line ("extension:" & Ada.Directories.Extension (Path));
   Put_Line ("base:" & Ada.Directories.Base_Name (Path));
   Put_Line ("composed:" & Ada.Directories.Compose
             ("demo/logs/old", "report.txt"));
end Dir_Parts;

The extension is txt without the dot, and the base name still contains the directory part. The second argument of Compose must be a simple name; passing "old/report.txt" raises Name_Error with the message "invalid simple name". Composing is preferable to joining strings with a slash, because it inserts the separator that the platform expects.

$ gnatmake -gnat2022 -gnatwa dir_parts.adb
$ ./dir_parts
simple:report.txt
parent:demo/logs/old
extension:txt
base:report
composed:demo/logs/old/report.txt

Kind, size and modification time

Kind returns a value of the enumeration type File_Kind: Directory, Ordinary_File, or Special_File. The type and the function share a name, so the image is written File_Kind'Image; Kind'Image would be read as an attempt to apply the attribute to the function. Size returns a File_Size, and Modification_Time returns an Ada.Calendar.Time.

dir_info.adb
with Ada.Text_IO;
with Ada.Directories;
with Ada.Calendar.Formatting;
use Ada.Text_IO;

procedure Dir_Info is
   use Ada.Directories;
   F : File_Type;
begin
   if not Exists ("demo") then
      Create_Directory ("demo");
   end if;
   Create (F, Out_File, "demo/notes.txt");
   Put_Line (F, "first line");
   Close (F);

   Put_Line ("kind:" & File_Kind'Image (Kind ("demo/notes.txt")));
   Put_Line ("size:" & File_Size'Image (Size ("demo/notes.txt")));
   Put_Line ("modified:" & Ada.Calendar.Formatting.Image
             (Modification_Time ("demo/notes.txt")));
   Put_Line ("dir kind:" & File_Kind'Image (Kind ("demo")));
end Dir_Info;

The file holds first line followed by a newline, which is eleven bytes. File_Size'Image begins with a blank for a non-negative value, so the output shows one space after the colon. The modification time is formatted here with Ada.Calendar.Formatting.Image; a later article in the series covers the calendar types in detail. The last line shows that the same Kind function also classifies a directory.

$ gnatmake -gnat2022 -gnatwa dir_info.adb
$ ./dir_info
kind:ORDINARY_FILE
size: 11
modified:2026-11-23 10:29:32
dir kind:DIRECTORY

Searching a directory

Start_Search opens a search over the entries of one directory. The pattern is matched against simple names, so it may contain * and ? but not a directory separator, and the search does not descend into subdirectories. More_Entries reports whether another entry is waiting, Get_Next_Entry retrieves it, and End_Search releases the search.

dir_search.adb
with Ada.Text_IO;
with Ada.Directories;
use Ada.Text_IO;

procedure Dir_Search is
   use Ada.Directories;

   S   : Search_Type;
   Ent : Directory_Entry_Type;
   F   : File_Type;
begin
   if not Exists ("reports") then
      Create_Directory ("reports");
   end if;
   Create (F, Out_File, "reports/jan.txt");
   Put_Line (F, "january");
   Close (F);

   Start_Search (S, "reports", "*.txt");
   while More_Entries (S) loop
      Get_Next_Entry (S, Ent);
      Put_Line (Simple_Name (Ent)
                & " size =" & File_Size'Image (Size (Ent)));
   end loop;
   End_Search (S);
end Dir_Search;

The program first creates a file to have something to find. The search then returns that one entry, whose simple name is jan.txt and whose size is eight bytes, the seven letters of january plus a newline. Size (Ent) and Modification_Time (Ent) work on a Directory_Entry_Type as well as on a name.

$ gnatmake -gnat2022 -gnatwa dir_search.adb
$ ./dir_search
jan.txt size = 8

An applied example: sizes and cleanup

A search becomes useful when it selects a group of files. The program below writes three files, searches for the two text files, adds their sizes, and then removes the whole directory. The sum uses File_Size rather than Integer, because that is the type that Size returns.

dir_report.adb
with Ada.Text_IO;
with Ada.Directories;
use Ada.Text_IO;

procedure Dir_Report is
   use Ada.Directories;

   Dir   : constant String := "reports";
   S     : Search_Type;
   Ent   : Directory_Entry_Type;
   Total : File_Size := 0;
   Count : Natural := 0;

   procedure Write_File (Name : String; Text : String) is
      F : File_Type;
   begin
      Create (F, Out_File, Name);
      Put_Line (F, Text);
      Close (F);
   end Write_File;
begin
   if not Exists (Dir) then
      Create_Directory (Dir);
   end if;
   Write_File (Dir & "/jan.txt", "january");
   Write_File (Dir & "/feb.txt", "february");
   Write_File (Dir & "/notes.dat", "misc");

   Start_Search (S, Dir, "*.txt");
   while More_Entries (S) loop
      Get_Next_Entry (S, Ent);
      Count := Count + 1;
      Total := Total + Size (Ent);
   end loop;
   End_Search (S);
   Put_Line ("files =" & Natural'Image (Count));
   Put_Line ("bytes =" & File_Size'Image (Total));

   Delete_File (Dir & "/jan.txt");
   Delete_File (Dir & "/feb.txt");
   Delete_File (Dir & "/notes.dat");
   Delete_Directory (Dir);
   Put_Line ("removed:" & Boolean'Image (not Exists (Dir)));
end Dir_Report;

Only the two .txt files are counted, so the result is two files and seventeen bytes; the .dat file is ignored by the pattern. Delete_File removes one file, and Delete_Directory removes an empty directory, which is why every file is deleted first. The search order is not defined, so the program reports a count and a total rather than a numbered list.

$ gnatmake -gnat2022 -gnatwa dir_report.adb
$ ./dir_report
files = 2
bytes = 17
removed:TRUE

Next steps

The next article introduces generics, which let one procedure or package work with several types without giving up the checks of the compiler. It starts with a generic swap procedure and builds up to a generic bounded stack.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have tested paths with Exists, created one directory and a nested tree, split a path into a simple name, parent, extension, and base name, read the kind, size, and modification time, searched with a pattern, and removed the files and directory again. The files article covers the contents of the files that the examples create.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.