CFDIRECTORY | |||||||||||
Description
Manages interactions with directories. |
|||||||||||
Category
File management tags | |||||||||||
Syntax<cfdirectory action = "directory action" directory = "directory name" name = "query name" filter = "list filter" mode = "permission" sort = "sort specification" newDirectory = "new directory name" recurse = "yes" or "no"> |
|||||||||||
See also
cffile |
|||||||||||
History
ColdFusion MX 7: Added the recurse attribute (named recursive in Alpha 1) and directory result-set column. ColdFusion MX:
|
|||||||||||
Usage
If you put ColdFusion applications on a server that is used by multiple customers, you must consider the security of files and directories that could be uploaded or otherwise manipulated with this tag by unauthorized users. For more information about securing ColdFusion tags, see Configuring and Administering ColdFusion MX. If action = "list", cfdirectory returns the following result columns, which you can reference in a cfoutput tag:
You can use the following result columns in standard CFML expressions, preceding the result column name with the query name: #mydirectory.name# #mydirectory.directory# #mydirectory.size# #mydirectory.type# #mydirectory.dateLastModified# #mydirectory.attributes# #mydirectory.mode#
The filter attribute specifies a pattern of one or more characters. All names that match that pattern are included in the list. On Windows systems, pattern matching ignores text case, on UNIX and Linux, pattern matches are case-sensitive. The following two characters have special meaning in the pattern and are called metacharacters:
The following table shows examples of patterns and file names that they match:
|
|||||||||||
Example<!--- EXAMPLE 1: Creating and Renaming Check that the directory exists to avoid getting a ColdFusion error message. ---> <cfset newDirectory = "otherNewDir"> <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "newDir"> <!--- Check to see if the Directory exists. ---> <cfif DirectoryExists(currentDirectory)> <!--- If TRUE then rename the directory. ---> <cfdirectory action = "rename" directory = "#currentDirectory#" newDirectory = "#newDirectory#" > <cfoutput> <p>The directory existed and the name has been changed to: #newDirectory#</ p> </cfoutput> <cfelse> <!--- If FALSE, create the directory. ---> <cfdirectory action = "create" directory = "#currentDirectory#" > <cfoutput><p>Your directory has been created.</p></cfoutput> </cfif> <!--- EXAMPLE 2: Deleting a directory Check that the directory exists and that files are not in the directory to avoid getting ColdFusion error messages. ---> <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "otherNewDir"> <!--- Check to see if the Directory exists. ---> <cfif DirectoryExists(currentDirectory)> <!--- If TRUE, check to see if there are files in the directory before deleting. ---> <cfdirectory action="list" directory="#currentDirectory#" name="myDirectory"> <cfif myDirectory.recordcount gt 0> <!--- If TRUE, delete the files from the directory. ---> <cfoutput> <p>Files exist in this directory. Either delete the files or code something to do so.</P> </cfoutput> <cfelse> <!--- Directory is empty - just delete the directory. ---> <cfdirectory action = "delete" directory = "#currentDirectory#"> <cfoutput> <p>The directory existed and has been deleted.</P> </cfoutput> </cfif> <cfelse> <!--- If FALSE, post message or do some other function. ---> <cfoutput><p>The directory did NOT exist.</p></cfoutput> </cfif> <!---EXAMPLE 3: List directories The following example creates both an array of directory names and a query that contains entries for the directories only. ---> <cfdirectory directory="C:/temp" name="dirQuery" action="LIST"> <!--- Get an array of directory names. ---> <cfset dirsArray=arraynew(1)> <cfset i=1> <cfloop query="dirQuery"> <cfif dirQuery.type IS "dir"> <cfset dirsArray[i]=dirQuery.name> <cfset i = i + 1> </cfif> </cfloop> <cfdump var="#dirsArray#"> <br> <!--- Get all directory information in a query of queries.---> <cfquery dbtype="query" name="dirsOnly"> SELECT * FROM dirQuery WHERE TYPE='Dir' </cfquery> <cfdump var="#dirsOnly#"> |
ACTION | |
Optional | |
Default value: "List"
|
DIRECTORY | |
Required | |
Absolute pathname of directory against which to perform action. You can use an IP address, as in the following example: <cfdirectory directory="//12.3.123.123/c_drive/" name="dirQuery" action="LIST"> |
NAME | |
Required if action = "list" | |
Name for output record set. |
FILTER | |
Optional if action = "list" | |
File extension filter applied to returned names; for example, *.cfm. One filter can be applied. |
MODE | |
Optional | |
Used with action = "create". Permissions. Applies only to UNIX and Linux. Octal values of chmod command. Assigned to owner, group, and other, respectively. For example:
|
SORT | |
Optional; used if action = "list" | |
Default value: "ASC"
Query column(s) by which to sort directory listing. Delimited list of columns from query output. To qualify a column, use:
For example: sort = "directory ASC, size DESC, datelastmodified" |
NEWDIRECTORY | |
Required if action = "rename" | |
New name for directory. |
RECURSE | |
Optional | |
Default value: "no"
Whether ColdFusion performs the action on subdirectories:
Valid for action="list" and action="delete". |