Как удалить каталог ROOT с FTP с помощью CFFTP - PullRequest
0 голосов
/ 01 апреля 2020

Мне нужно удалить каталог (папку) с FTP-сервера, используя coldfusion. Папки моего сервера выглядят так, как показано ниже:

/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'. Итак, если мы удалим папку root (jawa), то весь каталог и файлы.

Это возможные ребята?

1 Ответ

1 голос
/ 01 апреля 2020

Вот функция, которую я написал много лет go для выполнения sh этой задачи. Он использует <cfdirectory> с опцией recurse="yes". Это создает объект запроса, в котором я выполняю и только сначала удаляю файлы. Затем я запускаю второй l oop в обратном порядке, который затем удаляет папки. Этот метод хорошо работал без проблем в течение многих лет.

<!--- 
    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>
...