ASL
Loading...
Searching...
No Matches
Directory Class Reference

Detailed Description

This class allows enumerating the contents of a directory, its files and subdirectories, and doing file system actions (copy, move, delete).

This example lists the files of the c:/windows directory modified in the last 10 days:

Directory dir("c:/windows");
for (File& file : dir.files("*.dll"))
{
if (file.lastModified() > Date::now() - 10*Date::DAY)
cout << *file.name() << " size: " file.size() << endl;
}
An Array is a contiguous and resizable array of any type of elements.
Definition Array.h:77
static Date now()
Returns the current date-time.
This class allows enumerating the contents of a directory, its files and subdirectories,...
Definition Directory.h:54
Array< File > files(const String &which="*")
Returns the files in a directory, optionally matching a wildcard or several separated by '|'.
Definition Directory.h:96
Class File represents a file in the filesystem.
Definition File.h:63

In a Directory object, files() enumerates files, subdirs() enumerates subdirectories, and items() enumerates both. In all three a wildcard can be given as argument to filter the search.

The wildcard can contain more than one pattern separated by |, like dir.files("*.txt|*.doc"). If the directory actually refers to a file, then only that file is returned.

Each File object returned has the following members:

  • path(): the full path of this item
  • name(): the name of the item
  • directory(): the full directory containing the item
  • size(): the file size in bytes (or 0 if it is a directory)
  • lastModified(): a Date indicating the last modification time
  • creationDate(): a Date indicating the item's creation time

In fact they are File objects and any function of that class can be used on them.

File system operations are available here or in the File class:

Directory::create("/some_other_dir");
Directory::move("/path/file.txt", "/some_other_dir");
static bool move(const String &from, const String &to)
Moves or renames file from to to which can be a full name or a destination directory.
static bool create(const String &name)
Creates a new directory (and its ancestors if they don't exist), returns false on failure.

#include <Directory.h>

Public Member Functions

 Directory (const String &name)
 Constructs a directory from a relative or absolute path.
 
String name () const
 Returns the name of the directory.
 
String path () const
 Returns the full path of the directory.
 
String directory () const
 Returns the parent directory containing this directory.
 
bool exists () const
 Returns true if this directory exists, or false if it does not exist or refers to a file.
 
Array< Fileitems (const String &which="*", ItemType t=ALL)
 Returns the contents of a directory, optionally matching a wildcard.
 
Array< Filefiles (const String &which="*")
 Returns the files in a directory, optionally matching a wildcard or several separated by '|'.
 
Array< Filesubdirs (const String &which="*")
 Returns the subdirectories of a directory, optionally matching a wildcard.
 

Static Public Member Functions

static Array< Stringroots ()
 Returns a list of root paths (drives on windows, like "A:/", "C:/", or only "/" elsewhere)
 
static String current ()
 Returns the current working directory.
 
static bool change (const String &dir)
 Sets the current working directory.
 
static bool create (const String &name)
 Creates a new directory (and its ancestors if they don't exist), returns false on failure.
 
static bool createOne (const String &name)
 Creates a new directory, returns false on failure (e.g if its parent does not exist)
 
static String createTemp ()
 Creates a new temporary directory with an arbitrary name and returns its path.
 
static bool copy (const String &from, const String &to)
 Copies file from to to which can be a full name or a destination directory.
 
static bool move (const String &from, const String &to)
 Moves or renames file from to to which can be a full name or a destination directory.
 
static bool remove (const String &path)
 Deletes file or directory path (if it is a directory it must be empty)
 
static bool removeRecursive (const String &path)
 Removes the given directory with all its content recursively and returns true on success (USE WITH CARE!)
 
static Space freeSpace (const String &dir)
 Returns free space and total size of a file system.
 

The documentation for this class was generated from the following file: