Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.9k views
in Java FTP by (1k points)
hi,

I have some ftp remote directories that contain recursive files and subdirectories...

i want to be able to delete a given ftp directory and all its contents (files and sub directories) recursively and finally delete the directory itself .

i think this could be done using a ftp command and the quote() method .but i don't know which ftp command to use nor how to use it.

can someone show me some java code to do this ftp dir recursive delete ?

thanks.

3 Answers

0 votes
by (51.6k points)
There is no standard way of recursively deleting directories in FTP. Some FTP servers support directory deletion through SITE commands. For example, ProFTPD has the "SITE RMDIR {path}" command. You can invoke this command from FTPClient using the following code:
ftpClient.site("SITE RMDIR " + directoryName);

but again this is not a standard command and, though probably supported by a few different servers, it is not supported by most servers.

The most reliable way of doing it is to write a recursive method that walks the tree, deleting each file and directory individually.

- Hans (EnterpriseDT)
0 votes
by (1k points)
thank you for replying...
yes i tried to write my own recursive method ,but i'm still not able getting it work correctly.
below are 3 java methods.i'm using free edtftp version.
i call the method deleteDirectory(..) but it is not working..
can you help me fix my below code to make it work ?
thanks.


 public static void forceDelete(FTPClient client,FTPFile  file) throws Exception {
        if (file.isDir()) {
            deleteDirectory(client,file);
        } else {
           /* if (!client.exists(file.getName())) {
                throw new FileNotFoundException("File does not exist: " + file);
            }*/
            client.delete(file.getName());
           /* if (!file.delete()) {
                String message =
                    "Unable to delete file: " + file;
                throw new IOException(message);
            }*/
        }
    }
    //-----------------------------------------------------------------------
    /**
     * Recursively delete a directory.
     *
     * @param directory  directory to delete
     * @throws IOException in case deletion is unsuccessful
     */
    public static void deleteDirectory(FTPClient client,FTPFile directory) throws Exception {
       /* if (!client.exists(directory.getName())) {
            return;
        }*/

        cleanDirectory(client,directory);
        client.cdup();
        client.rmdir(directory.getName());
        /*if (!directory.delete()) {
            String message =
                "Unable to delete directory " + directory + ".";
            throw new IOException(message);
        }*/
    }

    /**
     * Clean a directory without deleting it.
     *
     * @param directory directory to clean
     * @throws IOException in case cleaning is unsuccessful
     */
    public static void cleanDirectory(FTPClient client,FTPFile directory) throws Exception {
       String filePath=directory.getName();
      String path=null;
      /*FTPFile f=(FTPFile)Utils.p_ftpFiles.get(filePath);
         if(f==null)
         return;*/
            
       path=(directory.getPath()==null)? ("/"+filePath):directory.getPath();
      
      // client.chdir(directory.getName());
       
       
       /*if (!client.exists(directory.getName())) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }*/

        if (directory.isDir() && !directory.isLink()) {
        client.chdir(path);
          //  String message = directory + " is not a directory";
           // throw new IllegalArgumentException(message);
       

        FTPFile[] files = client.dirDetails(path);
        if (files == null || files.length==0) {  // null if security restricted
           client.cdup();
            client.rmdir(directory.getName());
        }

       
        for (int i = 0; i < files.length; i++) {
           FTPFile file = files[i];           
            forceDelete(client,file);
           
        }

       
        }
    }
0 votes
by (51.6k points)
I forgot to mention that the commercial version of edtFTPj/PRO has methods for recursively deleting directories. As you've discovered it is a lot harder you might think. edtFTPj/PRO has a wealth of other features that will probably save you a lot of time and your boss a lot of money.

- Hans (EnterpriseDT)

Categories

...