1. Technology

Directory Helper Routines

Directory Related Delphi Functions and Procedures


When developing applications that need to operate on files and directories it is a common task to, for example, get the list of all sub-directories for a given directory, check if a directory is empty or delete a directory.

Delphi's RTL library provides dozens of directory (and file) related functions and procedures.

No metter whether you want to make sure a given directory exists or set the current directory ... Delphi has a ready made routine for you to use.

However, some routines are "missing": how to check if a directory is empty or how to get the parent folder for a given directory.

Directory / Folder Helper - Routines to Manipulate Directories

Here's a list of Delphi routines to help you operate on any given directory. Some originate from the RTL, some are "custom made". All routines are given with an example (source code) of usage.

Is Directory Empty
The IsDirectoryEmpty returns true if a directory is empty (containes no files, no sub-folders).

Directory Parent
The GetDirectoryParent retrieves the parent directory of the specified path.

Rename a Directory
RenameFile returns false if the file cannot be renamed (for example, if the application does not have permission to modify the file) or if the new name specified represents the name of an existing file. RenameFile can rename folders / directories also.

List SubDirectories for a Directory
The GetSubDirectories fills a TStrings with all the subdirectories for a given directory.

Get Directory Creation, Modified, Last Accessed and Last Write Date and Time
The DirectoryDateTimeInfo displays modified, last access, last write and created date time info for a given directory (or a file).

Get Files and Sub Directories
The ListDirectoryContent retrieves all files and sub folders for a specified directory.

Delete Full Directory
The DeleteTree completely deletes a directory regardless of whether the directory is filled or has subdirectories.

Directory Size
The DirectorySize retrieves the total size of all the files in a given directory.

Directory Exists
The DirectoryExists returns True if a specified directory exists.

Force Directories
The ForceDirectories creates all the folders along a given path.

Get Current Directory
The GetCurrentDir returns the name of the current directory.

Remove Directory
The RemoveDir deletes an empty folder.

Set Current Directory
The SetCurrentDir sets the current directory.

If you have some special directory-related function you would like to share, please send it to my email!

©2014 About.com. All rights reserved.