删除Coldfusion中特定文件夹中的所有空目录

时间:2011-05-20 05:48:42

标签: 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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

尽管我不是cf程序员,但是,如果我不是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