首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CFFTP从FTP中删除根目录

如何使用CFFTP从FTP中删除根目录
EN

Stack Overflow用户
提问于 2020-04-01 16:18:01
回答 1查看 102关注 0票数 0

我的服务器文件夹如下所示:

代码语言:javascript
复制
/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 ),那么整个目录和文件。

这些是可能的人吗?

EN

回答 1

Stack Overflow用户

发布于 2020-04-01 23:42:23

这是我多年前为完成此任务而编写的一个函数。它使用带有recurse="yes"选项的<cfdirectory>。这将创建一个查询对象,在该对象中,我只遍历并首先删除文件。然后,我反向运行第二个循环,然后删除文件夹。多年来,这种方法运行良好,没有出现任何问题。

代码语言:javascript
复制
<!--- 
    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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60966381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档