1
我正在處理需要從特定位置刪除所有空文件夾(目錄)的項目。我相信沒有直接的方法找到文件夾中的空導演。我只寫下面的方法來刪除層次結構中的空文件夾。任何人都可以有更好的解在Coldfusion中刪除特定文件夾中的所有空目錄
<cffunction name="deleteEmptyFolder" access="public" output="true" returntype="boolean">
<cfargument name="path" required="true" type="string" />
<cfset var qList="">
<cfset var qDir = "">
<cfset var qFiles = "">
<cfset var isEmpty = 1>
<!--- List Directory --->
<cfdirectory action="list" directory="#arguments.path#" recurse="no" name="qList">
<!--- get sub directory list --->
<cfquery name="qDir" dbtype="query">
select * from qList where type='Dir'
</cfquery>
<!--- Call recursive function to check directory empty or not --->
<cfloop query="qDir">
<!--- If sub directory not empty mark current directory as not empty. --->
<cfif not deleteEmptyFolder(qDir.directory & "\" & qDir.name)>
<cfset isEmpty=0>
</cfif>
</cfloop>
<!--- Check for file exists in current directory --->
<cfquery name="qFiles" dbtype="query">
select * from qList where type='File'
</cfquery>
<!--- If file exists mark as not empty --->
<cfif qFiles.recordCount gt 0>
<cfset isEmpty = 0>
</cfif>
<!--- If current directory empty then delete it --->
<cfif isEmpty>
<cfdirectory action="delete" recurse="false" directory="#arguments.path#">
</cfif>
<!--- Return empty status for current directory --->
<cfreturn isEmpty>
</cffunction>
我想cfdirectory刪除將刪除一個文件夾,即使它不是空的。這不會是他想要的。 – DefconRhall 2011-05-26 01:58:59
如果不爲空,則會引發錯誤,因此我使用try/catch或文件計數檢查。編輯:實際上看文檔,它可能會做一個完整的刪除,如果「遞歸」設置爲「是」,但它默認爲「否」 – 2011-05-26 15:31:16
啊,說明它,然後,我總是使用遞歸。不知道這是一個錯誤或預期的功能。 – DefconRhall 2011-05-26 15:51:36