
This language feature is only available from Ada 2005 on. Ada.Directories is a unit of the Predefined Language Environment since Ada 2005. It allows various manipulations and queries of the filesystem directories.
The Package Directories
Used for simple directory manipulation as defined in Ada 2005.
Traversing Directories
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.
functionCurrent_DirectoryreturnString;procedureSet_Directory (Directory :inString);functionExists (Name :inString)returnBoolean;functionContaining_Directory (Name :inString)returnString;
Enumerating Directory Entries
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.
typeSearch_Typeislimitedprivate;typeDirectory_Entry_Typeislimitedprivate;procedureStart_Search (Search :inoutSearch_Type; Directory :inString; Pattern :inString; Filter :inFilter_Type := (others=> True));procedureGet_Next_Entry (Search :inoutSearch_Type; Directory_Entry :outDirectory_Entry_Type);functionMore_Entries (Search :inSearch_Type)returnBoolean;procedureEnd_Search (Search :inoutSearch_Type);
The resulting Directory_Entry_Type has a full name, simple name, size, kind, and modification type.
functionSimple_Name (Directory_Entry :inDirectory_Entry_Type)returnString;functionFull_Name (Directory_Entry :inDirectory_Entry_Type)returnString;functionKind (Directory_Entry :inDirectory_Entry_Type)returnFile_Kind; -- Directory, Ordinary_File, Special_FilefunctionSize (Directory_Entry :inDirectory_Entry_Type)returnFile_Size;functionModification_Time (Directory_Entry :inDirectory_Entry_Type)returnAda.Calendar.Time;
withAda.Text_IO;withAda.Directories;useAda.Text_IO;useAda.Directories;procedureMainisDir : Directory_Entry_Type; Dir_Search : Search_Type; Curr_Dir : string := Current_Directory;beginPut("Current Directory: "); Put_Line(Curr_Dir); Start_Search(Search => Dir_Search, Directory => Curr_Dir, Pattern => "*.gpr");loopGet_Next_Entry(Dir_Search, Dir); Put(Full_Name(Dir)); Set_Col(50);ifKind(Dir) = Ordinary_FilethenPut(Size(Dir)'Image);endif; Set_Col(60); Put_Line(Kind(Dir)'Image);exitwhennotMore_Entries(Dir_Search);endloop; End_Search(Dir_Search);endMain;
Directory Manipulation
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.
procedureCreate_Directory (New_Directory :inString; Form :inString := "");procedureCreate_Path (New_Directory :inString; Form :inString := "");procedureRename (Old_Name, New_Name :inString);
procedureDelete_Tree (Directory :inString);procedureDelete_Directory (Directory :inString);procedureDelete_File (Name :inString);
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 "\").
procedureMainisDir : Directory_Entry_Type; Dir_Search : Search_Type;beginStart_Search(Search => Dir_Search, Directory => Curr_Dir, Pattern => ""); Create_Path("Foo"); Create_Path("Bar/Baz/Qux");loopGet_Next_Entry(Dir_Search, Dir); Put_Line(Full_Name(Dir));exitwhennotMore_Entries(Dir_Search);endloop; End_Search(Dir_Search); Delete_Directory("Foo"); Delete_Tree("Bar");endMain;
Exceptions
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 supported or directories are not traversable.
Status_Error :exceptionrenamesAda.IO_Exceptions.Status_Error; Name_Error :exceptionrenamesAda.IO_Exceptions.Name_Error; Use_Error :exceptionrenamesAda.IO_Exceptions.Use_Error; Device_Error :exceptionrenamesAda.IO_Exceptions.Device_Error;
Specification
-- Standard Ada library specification -- Copyright (c) 2003-2018 Maxim Reznik <reznikmm@gmail.com> -- Copyright (c) 2004-2016 AXE Consultants -- Copyright (c) 2004, 2005, 2006 Ada-Europe -- Copyright (c) 2000 The MITRE Corporation, Inc. -- Copyright (c) 1992, 1993, 1994, 1995 Intermetrics, Inc. -- SPDX-License-Identifier: BSD-3-Clause and LicenseRef-AdaReferenceManual -- -------------------------------------------------------------------------withAda.Calendar;withAda.IO_Exceptions;packageAda.Directoriesis-- Directory and file operations:functionCurrent_DirectoryreturnString;procedureSet_Directory (Directory :inString);procedureCreate_Directory (New_Directory :inString; Form :inString := "");procedureDelete_Directory (Directory :inString);procedureCreate_Path (New_Directory :inString; Form :inString := "");procedureDelete_Tree (Directory :inString);procedureDelete_File (Name :inString);procedureRename (Old_Name :inString; New_Name :inString);procedureCopy_File (Source_Name :inString; Target_Name :inString; Form :inString := ""); -- File and directory name operations:functionFull_Name (Name :inString)returnString;functionSimple_Name (Name :inString)returnString;functionContaining_Directory (Name :inString)returnString;functionExtension (Name :inString)returnString;functionBase_Name (Name :inString)returnString;functionCompose (Containing_Directory :inString := ""; Name :inString; Extension :inString := "")returnString; -- File and directory queries:typeFile_Kindis(Directory, Ordinary_File, Special_File);typeFile_Sizeisrange0 .. implementation_defined;functionExists (Name :inString)returnBoolean;functionKind (Name :inString)returnFile_Kind;functionSize (Name :inString)returnFile_Size;functionModification_Time (Name :inString)returnAda.Calendar.Time; -- Directory searching:typeDirectory_Entry_Typeislimitedprivate;typeFilter_Typeisarray(File_Kind)ofBoolean;typeSearch_Typeislimitedprivate;procedureStart_Search (Search :inoutSearch_Type; Directory :inString; Pattern :inString; Filter :inFilter_Type := (others=> True));procedureEnd_Search (Search :inoutSearch_Type);functionMore_Entries (Search :inSearch_Type)returnBoolean;procedureGet_Next_Entry (Search :inoutSearch_Type; Directory_Entry :outDirectory_Entry_Type);procedureSearch (Directory :inString; Pattern :inString; Filter :inFilter_Type := (others=> True); Process :notnullaccessprocedure(Directory_Entry :inDirectory_Entry_Type)); -- Operations on Directory Entries:functionSimple_Name (Directory_Entry :inDirectory_Entry_Type)returnString;functionFull_Name (Directory_Entry :inDirectory_Entry_Type)returnString;functionKind (Directory_Entry :inDirectory_Entry_Type)returnFile_Kind;functionSize (Directory_Entry :inDirectory_Entry_Type)returnFile_Size;functionModification_Time (Directory_Entry :inDirectory_Entry_Type)returnAda.Calendar.Time; Status_Error :exceptionrenamesAda.IO_Exceptions.Status_Error; Name_Error :exceptionrenamesAda.IO_Exceptions.Name_Error; Use_Error :exceptionrenamesAda.IO_Exceptions.Use_Error; Device_Error :exceptionrenamesAda.IO_Exceptions.Device_Error;privatepragmaImport (Ada, Directory_Entry_Type);pragmaImport (Ada, Search_Type);endAda.Directories;
See also
Wikibook
External examples
- Search for examples of
Ada.Directoriesin: Rosetta Code, GitHub or this Wikibook. - Search for any post related to
Ada.Directoriesin: Stack Overflow, comp.lang.ada or any Ada related page.
Tutorials
Ada Reference Manual
Ada 2005
Ada 2012
Open-Source Implementations
FSF GNAT
- Specification: a-direct.ads
- Body: a-direct.adb
drake
- Specification: directories/a-direct.ads
- Body: directories/a-direct.adb