0

I need to remove the Directory(folder) from FTP server using coldfusion. My server folders looks like below,

/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

My question is, how to remove the 'jawa' folder. Because all files and notes folder is under 'jawa' folder. So, If we delete the root folder ( jawa ) then the whole directory and files.

Are these possible guys?

jawahar N
  • 462
  • 2
  • 13
  • 1
    I suggest talking to the person who told you to do this and get more information as to the overall objective. While you are sorting this out, make a backup of that directory. – Dan Bracuk Apr 01 '20 at 13:12
  • 1
    Are you on a FTP connection or do you have direct and unrestricted access to the filesystem? – Alex Apr 01 '20 at 19:30

1 Answers1

1

Here's a function I wrote many years ago to accomplish this task. It uses <cfdirectory> with the recurse="yes" option. This creates a query object in which I loop through and first delete files only. Then I run a second loop in reverse which then deletes the folders. This method has worked well with no issues over many years.

<!--- 
    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>
user12031119
  • 1,228
  • 4
  • 14
  • Are you sure this will work with the webroot directory? I'm skeptical. – Dan Bracuk Apr 01 '20 at 16:48
  • @DanBracuk -- Good observation. This method doesn't delete the folder passed as an argument, it deletes the contents of the folder while leaving the folder name intact, but empty. So as an addition to this, a final step could be added to delete the folder that was passed in and it should still work fine, even if the folder was under the webroot. – user12031119 Apr 02 '20 at 03:01