-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystemObject.cfc
156 lines (125 loc) · 5.46 KB
/
FileSystemObject.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<cfcomponent hint="The FileSystemObject component provides methods to access and interact with the filesystem.">
<cfset variables.rootpath = "">
<cfset variables.directorySeparator = "">
<cffunction name="init" access="public" output="false" returnType="FileSystemObject" hint="This method initializes the component setting the root path and directory separator that are used by the other methods.">
<cfargument name="rootPath" type="string" required="false" default="" />
<cfargument name="directorySeparator" required="true" type="string" />
<cfset variables.directorySeparator = arguments.directorySeparator>
<cfif directoryExists(arguments.rootPath) and directoryExists(arguments.rootpath)>
<cfset variables.rootpath = arguments.rootpath>
</cfif>
<cfreturn this />
</cffunction>
<cffunction name="list" access="public" returntype="query" output="false" hint="The list method returns a list of all files in the directory path that match the given filter. It adds fullpath and package columns to the standard query returned by the cfdirectory tag. If the recurse argument is set to true, the method will also return the contents of all sub-directories of the rootpath.">
<cfargument name="recursive" default="false" type="boolean" />
<cfargument name="filter" default="" type="string" />
<cfargument name="prefix" required="yes">
<cfargument name="path" required="false" default="#variables.rootpath#" />
<cfset var qFiles = "">
<cfset var qSubDirectoryFiles = "">
<cfset var aPath = arrayNew(1)>
<cfset var aPackage = arrayNew(1)>
<cfset var aQueries = arrayNew(1)>
<cfset var i = 0>
<cfset var packageStart = "">
<cfset var subPath = "">
<cfset var package = "">
<cfset filter = lcase(replace(arguments.filter,'*','%','all'))>
<cfdirectory action="list" directory="#arguments.path#" name="qFiles">
<cfif qFiles.recordcount>
<cfset subPath = prefix & directorySeparator & replaceNoCase(arguments.path,variables.rootpath,'')>
<cfset package = listChangeDelims(subPath,'.','/\')>
<cfset arraySet(aPath,1,qFiles.recordcount,arguments.path)>
<cfset arraySet(aPackage,1,qFiles.recordcount,package)>
</cfif>
<cfset queryAddColumn(qFiles,'fullpath',aPath)>
<cfset queryAddColumn(qFiles,'package',aPackage)>
<cfif arguments.recursive>
<cfloop query="qFiles">
<cfif qFiles.type is 'dir'>
<cfset arrayAppend(aQueries,this.list(true,arguments.filter,arguments.prefix,arguments.path & variables.directorySeparator & qFiles.name))>
</cfif>
</cfloop>
<cfloop from="1" to="#arrayLen(aQueries)#" index="i">
<cfset q = aQueries[i]>
<cfif q.recordCount>
<cfquery dbtype="query" name="qFiles">
SELECT *
FROM qFiles
<cfif len(arguments.filter)>
WHERE LOWER(qFiles.name) LIKE('#filter#')
</cfif>
UNION
SELECT *
FROM q
<cfif len(arguments.filter)>
WHERE LOWER(q.name) LIKE('#filter#')
</cfif>
ORDER BY fullpath
</cfquery>
</cfif>
</cfloop>
</cfif>
<cfquery dbtype="query" name="qFiles">
SELECT *
FROM qFiles
<cfif len(arguments.filter)>
WHERE LOWER(qFiles.name) LIKE('#filter#')
</cfif>
</cfquery>
<cfreturn qFiles>
</cffunction>
<!---
Mimics the cfdirectory, action="list" command.
Updated with final CFMX var code.
Fixed a bug where the filter wouldn't show dirs.
@param directory The directory to list. (Required)
@param filter Optional filter to apply. (Optional)
@param sort Sort to apply. (Optional)
@param recurse Recursive directory list. Defaults to false. (Optional)
@return Returns a query.
@author Raymond Camden ([email protected])
@version 2, April 8, 2004
--->
<cffunction name="directoryList" output="false" returnType="query">
<cfargument name="directory" type="string" required="true">
<cfargument name="filter" type="string" required="false" default="">
<cfargument name="sort" type="string" required="false" default="">
<cfargument name="recurse" type="boolean" required="false" default="false">
<!--- temp vars --->
<cfargument name="dirInfo" type="query" required="false">
<cfargument name="thisDir" type="query" required="false">
<cfset var path="">
<cfset var temp="">
<cfif not recurse>
<cfdirectory name="temp" directory="#directory#" filter="#filter#" sort="#sort#">
<cfreturn temp>
<cfelse>
<!--- We loop through until done recursing drive --->
<cfif not isDefined("dirInfo")>
<cfset dirInfo = queryNew("attributes,datelastmodified,mode,name,size,type,directory")>
</cfif>
<cfset thisDir = directoryList(directory,filter,sort,false)>
<cfif server.os.name contains "Windows">
<cfset path = "\">
<cfelse>
<cfset path = "/">
</cfif>
<cfloop query="thisDir">
<cfset queryAddRow(dirInfo)>
<cfset querySetCell(dirInfo,"attributes",attributes)>
<cfset querySetCell(dirInfo,"datelastmodified",datelastmodified)>
<cfset querySetCell(dirInfo,"mode",mode)>
<cfset querySetCell(dirInfo,"name",name)>
<cfset querySetCell(dirInfo,"size",size)>
<cfset querySetCell(dirInfo,"type",type)>
<cfset querySetCell(dirInfo,"directory",directory)>
<cfif type is "dir">
<!--- go deep! --->
<cfset directoryList(directory & path & name,filter,sort,true,dirInfo)>
</cfif>
</cfloop>
<cfreturn dirInfo>
</cfif>
</cffunction>
</cfcomponent>