2011-05-20 132 views
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> 

回答

2
<cffunction name="deleteEmptyFolders" output="false"> 
    <cfargument name="path" required="true" /> 
    <cfset var subfolders = "" /> 
    <cfdirectory name="subfolders" action="list" directory="#path#" type="dir" /> 
    <cfloop query="subfolders"> 
     <cfset deleteEmptyFolders("#path#/#subfolders.name#") /> 
    </cfloop> 
    <cftry> 
     <cfdirectory action="delete" directory="#path#" /> 
     <cfcatch></cfcatch> 
    </cftry> 
</cffunction> 

編輯的取幫助:錯誤美中不足的是那裏只是爲了讓代碼更簡單並避免另一個文件列表調用。你也可以用這個代替...

<cfdirectory name="files" action="list" directory="#path#" /> 
<cfif not files.recordcount> 
    <cfdirectory action="delete" directory="#path#" /> 
</cfif> 
+0

我想cfdirectory刪除將刪除一個文件夾,即使它不是空的。這不會是他想要的。 – DefconRhall 2011-05-26 01:58:59

+0

如果不爲空,則會引發錯誤,因此我使用try/catch或文件計數檢查。編輯:實際上看文檔,它可能會做一個完整的刪除,如果「遞歸」設置爲「是」,但它默認爲「否」 – 2011-05-26 15:31:16

+0

啊,說明它,然後,我總是使用遞歸。不知道這是一個錯誤或預期的功能。 – DefconRhall 2011-05-26 15:51:36

1

下它會達到目的,雖然我不是一個CF程序員

<!---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 whether the directory exists. ---> 
<cfif DirectoryExists(currentDirectory)> 
    <!--- If yes, check whether there are files in the directory before deleting. ---> 
    <cfdirectory action="list" directory="#currentDirectory#" 
    name="myDirectory"> 
    <cfif myDirectory.recordcount gt 0> 
    <!--- If yes, 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 no, post message or do some other function. ---> 
    <cfoutput><p>The directory did NOT exist.</p></cfoutput> 
</cfif> 

ANSWER SOURCE

相關問題