我的服务器文件夹如下所示:
/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”文件夹下。所以,如果我们删除根文件夹( jawa ),那么整个目录和文件。
这些是可能的人吗?
发布于 2020-04-01 23:42:23
这是我多年前为完成此任务而编写的一个函数。它使用带有recurse="yes"选项的<cfdirectory>。这将创建一个查询对象,在该对象中,我只遍历并首先删除文件。然后,我反向运行第二个循环,然后删除文件夹。多年来,这种方法运行良好,没有出现任何问题。
<!---
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>https://stackoverflow.com/questions/60966381
复制相似问题