我需要使用Coldfusion从FTP服务器删除目录(文件夹)。我的服务器文件夹如下所示,
/jawa/notes/test.cfm
/jawa/notes/test1.cfm
/jawa/notes/test2.cfm
/jawa/notes/test3.cfm
/jawa/test1.cfm
/jawa/test2.cfm
/jawa/test3.cfm
我的问题是,如何删除“ jawa”文件夹。因为所有文件和注释文件夹都在“ jawa”文件夹下。因此,如果我们删除根目录文件夹(limba),则删除整个目录和文件。
这些人可能吗?
<cfdirectory>
和recurse="yes"
选项。这将创建一个查询对象,我在其中循环浏览并仅删除文件。然后我反向运行第二个循环,然后删除文件夹。多年来,此方法一直有效,没有任何问题。<!---
Private function that will clear a specified directory of all files, folders,
subfolders and files contained in subfolders.
--->
<cffunction name="clearFolder" access="private" returntype="string" output="no">
<cfargument name="dirPath" type="string" required="yes">
<!---
Step 1: Loop through all the files in the specified directory/subdirectories
and delete the files only.
--->
<cfdirectory action="list" name="qDirListing" directory="#dirPath#" recurse="yes">
<cfloop query="qDirListing">
<cfif qDirListing.type eq "file">
<cftry>
<cffile action="delete" file="#qDirListing.directory#\#qDirListing.name#">
<cfcatch type="any">
<cfreturn "failure: Error deleting file #qDirListing.directory#\#qDirListing.name#">
</cfcatch>
</cftry>
</cfif>
</cfloop>
<!---
Step 2: Now that the files are cleared from all the directories, loop through all
the directories and delete them. Note that you need to loop backwards through
the result set since the subdirectories need to be deleted before the parent
directories can be deleted.
--->
<cfdirectory action="list" name="qDirListing" directory="#dirPath#" recurse="yes">
<cfloop from="#qDirListing.recordCount#" to="1" step="-1" index="i">
<cftry>
<cffile action="delete" file="#qDirListing['directory'][i]#\#qDirListing['name'][i]#">
<cfcatch type="any">
<cfreturn "failure: Error deleting file #qDirListing['directory'][i]#\#qDirListing['name'][i]#">
</cfcatch>
</cftry>
</cfloop>
<cfreturn "">
</cffunction>