2011-05-20 126 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

相关问题