Working with Diredories ColdFusion Help

Up to this point, you have considered specifically how to work with files, butnot directories. The CFDIRECTORYtag provides the ability to create, delete, rename, and list the contents of directories on the ColdFusion server. Like the CFFILE tag, the action taken by the CFDIRECTORYtag is controlled by the ACTIONattribute, which can take four possible values:

Create   Creates a new directory
Delete    Deletes an existing directory
List         Lists the contents of an existing directory
Rename Renames an existing directory

Creating a Directory

Directories can be created using the ACTION=”Create” attribute and the DIRECTORY attribute, which specifies the full path and name of the directory to create. An optional MODEattribute specifies the mode perrrussions for the directory when using ColdFusion on Solaris, and takes octal triplets like those used in the Unix chmod command. For instance, to create a new direcjory called c: \newdi r, you could use the following tag:

<CFDIRECTORY ACTION=’Create’
DIRECTORY=’c:\newdir’)

Deleting a Directory

Deleting a directory combines the same DIRECTORYattribute used in creating a directory with ACTION=”De1ete “:

<CFDIRECTORY ACTION=’Delete’
DIRECTORY=’c:\newdir’)

Renaming a Directory

When renaming a directory using ACTION=n Rename”, two additional attributes are used:

DIRECTORY Specifies the name and path of the directory to be renamed
NEWDIRECTORY Specifies the new name and path for the directory

The commands for these attributes would be written as follows:

<CFDIRECTORY ACTION-‘Rename’
DIRECTORY=’c:\newdir’ NEWDIRECTORY-‘c:\newdirrenamed’)

Listing the Contents of a Directory

The ACTION=’List’ attribute enables the contents of a directory to be listed in the form of a query result set with five or six columns. The query result set can be used in the same way as any other query, including those created using CFQUERYT.he columns are described in the following list:

Attributes  Any attributes applied to a file DateLastModi fied The date on which the file or directory was last modified
Mode. The octal triplet representing the permissions for a file (available only on Solaris servers)
Name The name of the directory entry
Size The size of the directory entry
Type Type of entry (F for a file or 0 for a directory)
The CFDIRECTORYtag itself can take the following attributes:
DIRECTORY The name and path of the directory of which to list the contents. This is a required attribute.
NAME The name of the query result set. This is a required attribute.
FILTER An optional value specifying a file extension filter to apply to the returned names. For instance, specifying * .txt causes only files with the TXT extension to be included in the results.
SORT An optional attribute to specify how to sort the returned list of files and directories. This attribute is a comma-separated list of columns from the query- output that serve as sort keys. For instance, sorting by name and then by size can be done with SORT=”Name,Si ze’. By default, each column is sorted in ascending order, but this can be controlled with ASCfor ascending order and DESCfor descending order: SORT=”NameDESC, Size ASC”.

Using the information returned from a list query, you could quickly write a template to display the contents of a directory as a Web page. For instance, Listing 19.1 displays the contents of the c: temp directory on a ColdFusion server.

Posted on November 14, 2015 in File Management

Share the Story

Back to Top
Share This