ASL
Directory Class Reference

Detailed Description

This class allows enumerating the contents of a directory: its files and subdirectories.

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;
}
static Date now()
Returns the current date-time.

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.

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.

#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.
 
const Array< Fileitems (const String &which="*", ItemType t=ALL)
 Returns the contents of a directory, optionally matching a wildcard.
 
const Array< Filefiles (const String &which="*")
 Returns the files in a directory, optionally matching a wildcard.
 
const Array< Filesubdirs (const String &which="*")
 Returns the subdirectories of a directory, optionally matching a wildcard.
 

Static Public Member Functions

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!)
 

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