Note: This class is not available with the PO_NO_FILESYSTEM parse option.
The Dir class allows Qore programs to list and manipulate directories.
Directory objects can be created/opened with a specific character encoding. This means that any entry read from the directory will be tagged with the directory's character encoding. If no character encoding is given the default Qore character encoding is assumed.
Table 4.148. Dir Method Overview
Method | Except? | Description |
|---|---|---|
| N | Create a Directory object with optional character encoding. It points to the directory the script is started. |
N | Destroys the directory object. | |
N | Creates a new directory object with the same character encoding specification and the same path as the original. | |
| N | Change the path of the directory. The path specification can be relative or absolute (with leading '/'). This path does not necessarily need to exist. |
N | Returns a string giving the current path of the object, stripped of all '.' and '..' entries or NOTHING if no path is set. | |
N | Checks if the path in the object is an openable directory. | |
| Y | Try to create all directories of the object if they do not exist yet; if any errors occur an exception is thrown. |
| Y | Change the ownership of the directory to the given user. |
| Y | Change the group membership of the directory to the given group. |
| Y | Change the permissions of the directory to the given mode; if any errors occur an exception is thrown. |
Y | Returns a Stat Hash about the directory's status or throws an exception if any errors occur. | |
| Y | Create a subdirectory with the name in the directory; if any errors occur an exception is thrown. |
| Y | Delete an empty subdirectory with the name in the directory; if any errors occur an exception is thrown. |
Y | Returns a Stat List about the directory's status or throws an exception if any errors occur. | |
Y | Returns a Filesystem Status Hash about the directory's filesystem status or throws an exception if any errors occur. | |
Y | Get all entries in this directory, except '.' and '..' directories (takes an optional regular expression filter); if any errors occur an exception is thrown. | |
| Y | Get all entries in the directory which are not subdirectories (takes an optional regular expression filter); if any errors occur an exception is thrown. |
| Y | Get all entries in the directory which are subdirectories, except '.' and '..' directories (takes an optional regular expression filter); if any errors occur an exception is thrown. |
| Y | Get a Dir object as an subdir entry of the current directory. |
| Y | Get a File object which represents a file in the directory. |
| Y | Remove (unlink) a file in this directory. |
Creates a Dir object. It accepts one optional argument that will set the default character encoding encoding for the directory.
Dir::constructor(string$encoding)
my Dir $d("UTF-8");Table 4.149. Arguments for Dir::constructor()
Argument | Description |
|---|---|
| The character encoding for the directory. Any entry of the directory will be converted to this character encoding if necessary, and entries read will be tagged with this encoding. If no encoding is given, the default encoding is used. |
Creates a new Dir object with the same character encoding specification and path as the original.
my Dir $d2 = $d.copy();Change the directory of the Dir object to the path; you can use either an absolute path (leading with '/') or a directory realtive to the actual path.
boolDir::chdir(string$path)
if ($d.chdir("../doc")) {
printf("the directory does not exist or is not readable\n");
}Returns True if the path in the Dir object points to a directory that already exists and is openable by the current user.
if (!$d.exists()) {
printf("the directory %s does not exist or cannot be opened\n", $d.path());
exit(-1);
}Table 4.153. Return Value for Dir::exists()
Return Type | Description |
|---|---|
True if the directory exists and is openable. |
Create the whole directory tree the Dir object points to, if it does not exist till now.
intDir::create(softint$mode = 0777)
if (!$d.exists()) {
printf("directory '%s' does not exist; creating...\n", $d.path());
$cnt = $d.create();
}Table 4.154. Arguments for Dir::create()
Argument | Description |
|---|---|
| The mode of the directory. |
Table 4.156. Exceptions Thrown by Dir::create()
err | desc |
|---|---|
| One of the directories in the path could not be created. |
Change the ownership of the directory.
nothingDir::chown(softint$uid)
nothingDir::chown(string$username)
$d.chown("nobody");Table 4.158. Exceptions Thrown by Dir::chown()
err | desc |
|---|---|
| Parameter error. |
| The error occoured in the chown() call. |
| This exception is thrown when the method is not available on the runtime platform; for maximum portability, check the constant |
Change the group membership of the directory.
nothingDir::chgrp(softint$gid)
nothingDir::chgrp(string$groupname)
$d.chgrp("nogroup");Table 4.160. Exceptions Thrown by Dir::chgrp()
err | desc |
|---|---|
| Parameter error. |
| Error occoured during the change group chown() system call. |
| This exception is thrown when the method is not available on the runtime platform; for maximum portability, check the constant |
Change the mode of the directory.
nothingDir::chmod(softint$mode)
$d.chmod(0711); # set mode to u(rwx) and go(x)
Table 4.162. Exceptions Thrown by Dir::chmod()
err | desc |
|---|---|
| The error returned from the chmod() system call. |
Returns a hash of directory status values for the current directory.
If any errors occur, a DIR-HSTAT-ERROR exception is thrown.
my hash $h = $dir.hstat();Table 4.164. Exceptions Thrown by Dir::hstat()
err | desc |
|---|---|
|
|
Make a subdirectory in the Dir object's path. There are no path info allowed (the '/'). If no mode is given the mode 0777 is used.
nothingDir::mkdir(string$name, softint$mode = 0777)
$d.mkdir("newSubDir", 0755);Table 4.166. Exceptions Thrown by Dir:mkdir()
err | desc |
|---|---|
| Parameter error. |
| The error returned from the mkdir() system call. |
Remove a subdirectory from the Dir object's path. There is no pathinfo allowed in the name (the '/' delimiter).
nothingDir::rmdir(string$subdir)
$d.rmdir("emptySubdir");Table 4.167. Arguments for Dir::rmdir()
Argument | Description |
|---|---|
| The name of the directory to be removed. |
Table 4.168. Exceptions Thrown by Dir::rmdir()
err | desc |
|---|---|
| Parameter error. |
| The error returned from the rmdir() system call. |
Returns a list of file status values for the current directory.
If any errors occur, a DIR-STAT-ERROR exception is thrown.
See also static File::stat().
my int $mode = $dir.stat()[2];Table 4.170. Exceptions Thrown by Dir::stat()
err | desc |
|---|---|
|
|
Returns a hash of filesystem status values for the current directory.
If any errors occur, a DIR-STATVFS-ERROR exception is thrown.
See also static File::statvfs().
my hash $h = $dir.statvfs()Table 4.171. Return Values for Dir::statvfs()
Return Type | Description | |
|---|---|---|
Returns a hash of filesystem status values current file; an exception is thrown if any errors occur. See also static File::statvfs(). See Filesystem Status Hash for a description of the hash returned by this function. |
Table 4.172. Exceptions Thrown by Dir::statvfs()
err | desc |
|---|---|
|
|
| This exception is thrown when the method is not available on the runtime platform; for maximum portability, check the constant |
List all entries in the directory of the Dir object. It supresses the '.' and '..' directory. An optional regular expression string can be passed to filter the values returned; if this argument is given then only entries that match the regular expression will be returned.
foreachmy string $e in ($d.list()) { printf("entry: %s\n"); }
Table 4.173. Arguments for Dir::list()
Argument | Description |
|---|---|
An optional regular expression string used to filter the arguments followed by bitwise-or'ed regex option constants. |
Table 4.174. Return Value for Dir::list()
Return Type | Description |
|---|---|
A list of Strings with the directories content. |
Table 4.175. Exceptions Thrown by Dir::list()
err | desc |
|---|---|
| The error returned from the readdir() system call. |
List all entries in the directory of the Dir object, which are not subdirectories. An optional regular expression string can be passed to filter the values returned; if this argument is given then only entries that match the regular expression will be returned.
listDir::listFiles(string$regex)
listDir::listFiles(string$regex, int$options)
foreachmy string $e in ($d.listFiles()) { printf("entry: %s\n"); }
Table 4.176. Arguments for Dir::listFiles()
Argument | Description |
|---|---|
An optional regular expression string used to filter the arguments followed by bitwise-or'ed regex option constants. |
Table 4.177. Return Value for Dir::listFiles()
Return Type | Description |
|---|---|
A list of strings with the file names as elements. |
Table 4.178. Exceptions Thrown by Dir::listFiles()
err | desc |
|---|---|
| The error returned from the readdir() system call. |
List all entries in the directory of the Dir object that are subdirectories, not including the '.' and '..' directories. An optional regular expression string can be passed to filter the values returned; if this argument is given then only entries that match the regular expression will be returned.
listDir::listDirs(string$regex)
listDir::listDirs(string$regex, int$options)
foreachmy string $e in ($d.listDirs()) { printf("entry: %s\n"); }
Table 4.179. Arguments for Dir::listDirs()
Argument | Description |
|---|---|
An optional regular expression string used to filter the arguments followed by bitwise-or'ed regex option constants. |
Table 4.180. Return Value for Dir::listDirs()
Return Type | Description |
|---|---|
A list of strings with the directory names found. |
Table 4.181. Exceptions Thrown by Dir::listDirs()
err | desc |
|---|---|
| The error returned from the readdir() system call. |
Return a Dir object in the directory of the Dir object. The dirname does not allow path information (the '/').
DirDir::openDir(string$subdir)
DirDir::openDir(string$subdir, string$encoding)
# open a subdir for working with my Dir $sd = $d.openDir("mysubdir", "ISO-8859-1"); my list $sd_list = $sd.list();
Table 4.182. Arguments for Dir::openDir()
Argument | Description |
|---|---|
The name of the subdirectory. The directory must not exist and can be created with create() afterwards. If no encoding is given, the default encoding is used. |
Table 4.183. Return Value for Dir::openDir()
Return Type | Description |
|---|---|
The Dir object created for the directory. |
Table 4.184. Exceptions Thrown by Dir::openDir()
err | desc |
|---|---|
| The directory name to be opened contains path information ('/'). |
Create or open a File object in the directory of the Dir object. The filename does not allow path information (the '/'). Uses the File::open2() method.
FileDir::openFile(string$name, softint$flags = O_RDONLY, softint$mode = 0666)
FileDir::openFile(string$name, softint$flags = O_RDONLY, softint$mode = 0666, string$encoding)
# open a file for writing in the directory and set the mode to
# 0644 and the encoding to UTF-8
my $f = $d.openFile("myfile.txt", O_CREAT|O_WRONLY, 0644, "UTF-8");Table 4.185. Arguments for Dir::openFile()
Argument | Description |
|---|---|
| The name of the file in the current directory. |
Flags that determine the way the file is accessed, see File Constants for more information. If this argument is not given, | |
| Permission bits for when the file is to be created (default: 0666) |
| The name of the default character encoding for this file; if this argument is not given, the file will be tagged with the default character encoding for the process. |
Table 4.186. Return Value for Dir::openFile()
Return Type | Description |
|---|---|
Object | The File object created or opened in the directory. |
Table 4.187. Exceptions Thrown by Dir::openFile()
err | desc |
|---|---|
| The file name to be opened contains path information ('/'). |
File Exceptions | Exceptions thrown by the File::open2() call. |
Remove the file with the given name in the Dir object's directory.
boolDir::removeFile(string$name)
$d.removeFile("myTestFile.dat");Table 4.188. Arguments for Dir::removeFile()
Argument | Description |
|---|---|
| The name of the file in the directory. |
Table 4.189. Return Value for Dir::removeFile()
Return Type | Description |
|---|---|
True if the file was present and could be removed. |
Table 4.190. Exceptions Thrown by Dir::removeFile()
err | desc |
|---|---|
| Parameter error. |
| The error returned by the unlink() system call. |
There are no comments yet