Dir class

The Dir class provides access to file system directory structures and their contents in a platform-independent way. It provides a means of listing directory content, creating filenames with proper path separators, etc.

Enum FilterSpec

This enum describes the filtering options available to Dir for entryList(). The filter value is specified by OR-ing together values from the following list.

FilterSpec

Description

Dir.Dirs

List directories only

Dir.Files

List files only

Dir.Drives

List disk drives only

Dir.NoSymLinks

Do not list symbolic links

Dir.All

List directories, files, drives and symlinks (this does not list broken symlinks unless you specify System)

Dir.TypeMask

A mask for the the Dirs, Files, Drives and NoSymLinks flags

Dir.Readable

List files for which the application has read access

Dir.Writable

List files for which the application has write access

Dir.Executable

List files for which the application has execute access; executables must be combined with Dirs or Files

Dir.RWEMask

A mask for the Readable, Writable and Executable flags

Dir.Modified

Only list files that have been modified

Dir.Hidden

List hidden files

Dir.System

List system files

Dir.AccessMask

A mask for the Readable, Writable, Executable Modified, Hidden and System flags

Enum SortSpec

This enum describes the sort options available to Dir for entryList(). The sort value is specified by OR-ing together values from the following list.

SortSpec

Description

Dir.Name

Sort by name

Dir.Time

Sort by time (modification time)

Dir.Size

Sort by file size

Dir.Unsorted

Do not sort

Dir.SortByMask

A mask for Name, Time and Size

Dir.DirsFirst

Put the directories first, then the files

Dir.Reversed

Reverse the sort order

Dir.IgnoreCase

Sort case-insensitively

Static properties

current : String

The current directory.

home : String

The home directory.

root : String

The root directory.

drives : String[ ]

An Array of strings containing the drive names (c:, d:, etc).

Static functions

The Utility module uses "/" as a directory separator throughout (although it understands the conventions of the platform it is used on). If you are working with paths, use "/" within your code, and use convertSeparators() when you want to display a path to the user.

cleanDirPath( filePath : String ) : String

Removes all multiple directory separators "/" and resolves any "."s or ".."s found in the path filePath.

convertSeparators( pathName : String ) : String

Returns pathName with the "/" separators converted to separators that are appropriate for the underlying operating system.

Checking for arrival

This function checks if a folder and all of its files have "arrived". A folder has arrived if the number of files and subfolders in the folder (recursively) has not changed during the stability period, and if all of these items have arrived individually. See also ../reference/r_file_class.html#checking_for_arrival in the File class documentation.

hasArrived( filePath : String, stabilitySeconds : Number ) : Boolean [static]
hasArrived( stabilitySeconds : Number ) : Boolean

Returns true if it can be assumed that the folder and its contents have arrived according to the heuristic describe above; otherwise returns false. The stability period is specified in seconds (as a floating point number).

The length of the required stability period depends on the nature and size of the file, on the process writing the file and on the performance and workload of the computer system(s) involved. In most cases a stability period of 1 second is both sufficient and acceptable (in terms of delay). When the file is produced by a slow process or transferred across a slow network, a much longer stability period may be required.

Note:

A hasArrived() function often returns only after a delay of at least the stability period. However if it can be determined right away that the file has not yet arrived, the function returns immediately. Thus it is never acceptable to invoke a hasArrived() function in a tight loop.

The following table describes the two most important use cases for dealing with an external application through the hasArrived() functions. In fact, the guidance presented in the table does not depend on the employed arrival/ready detection mechanism.

Model

Description

Implementation

Synchronous

If the external process is synchronous, resource-intensive and hosted on the same computer as Switch, the jobArrived entry point should block until the process is finished (so that Switch can correctly manage the number of external processes running in parallel)

jobArrived entry point contains a waiting loop that repeatedly calls hasArrived() followed by Environment.sleep() to introduce additional delay

Asynchronous

If the external process is asynchronous (i.e. you want to feed it the next job even if the previous job hasn't been processed), if it depends on user interaction or if it potentially takes a long time without using a lot of resources on the local computer, the jobArrived entry point should return immediately and leave the follow-up to the timerFired entry point

timerFired entry point simply calls hasArrived() once, since it is automatically executed repeatedly with a predefined interval

Constructor

Dir( path : String )

Creates a directory object for path path. If path is empty, the current directory is used.

Properties

name : String

Contains the name of the directory; this is not the same as the path, e.g. a directory with the name "mail", might have the path "c:/spool/mail".

path : String

Contains the path, this may contain symbolic links, but never contains redundant ".", "..", or multiple separators.

absPath : String

Contains the absolute path (a path that starts with "/" or with a drive specification), which may contain symbolic links, but never contains redundant ".", "..", or multiple separators.

canonicalPath : String

Contains the canonical path, i.e. a path without symbolic links or redundant "." or ".." elements.

readable : Boolean

True if the directory is readable; otherwise false.

exists : Boolean

True if the directory exists; otherwise false.

Member functions

filePath( fileName : String ) : String

Returns the path name of the file fileName in the directory.

absFilePath( fileName : String ) : String

Returns the absolute path name of the file fileName in the directory.

cd( dirName : String )

Changes the Dir's directory to dirName if possible; otherwise throws an exception.

cdUp()

Changes directory by moving one directory up from the Dir's current directory if possible; otherwise throws an exception.

entryList( filter : String, filterSpec : Number, sortSpec : Number ) : String[ ]

Returns a list of the names of all the files and directories in the directory, ordered in accordance with sortSpec (see enum SortSpec) and filtered in accordance with filterSpec (see enum FilterSpec).

For example:

var profiles = profilesDir.entryList("*.icc", Dir.Files, Dir.Name);

Gets a list of ICC profiles in the specified directory, sorted by filename.

var writableProfiles = profilesDir.entryList("*.icc", Dir.Files|Dir.Writable, Dir.Name);

Same as above but the list is restricted to ICC profiles for which the application has write access.

mkdir( dirName : String )

Creates the directory dirName if possible; otherwise throws an exception.

mkdir()

Creates this directory if possible; otherwise throws an exception.

mkdirs( dirName : String )

Creates the directory tree dirName if possible; otherwise throws an exception.

mkdirs()

Creates this directory tree if possible; otherwise throws an exception.

rmdir( dirName : String )

Deletes the directory dirName if possible; otherwise throws an exception.

rmdir()

Deletes this directory if possible; otherwise throws an exception.

rmdirs( dirName : String )

Deletes the directory structure dirName if possible; otherwise throws an exception.

rmdirs()

Deletes this directory structure if possible; otherwise throws an exception.

fileExists( fileName : String ) : Boolean

Returns true if the file fileName exists; otherwise returns false.

setCurrent()

Sets the application's current working directory to this directory if possible; otherwise throws an exception.