aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to remove remote non-empty directory in FTP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to remove remote non-empty directory in FTP" Watch "How to remove remote non-empty directory in FTP" New topic
Author

How to remove remote non-empty directory in FTP

Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
Hi all,

I am using virgoFtp, it's a open source FTP client made in SWT. I am facing a problem that with this application i am not able to delete a non empty directory. I am sending 'RMD' command to delete directory as 'connect.sendCmd("RMD " + directory)' . It works fine for an empty directory but with having any content it gives me an error 'directory not empty' .
Can sombody tell me what mistake i am making or there is some other command to delete a non empty directory. I also tried 'RMD -rf <directory name>', then i gets an error message 'command not understand'.

with regards,
Pradeep Sharma
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You will have to recursively delete the folder. With java.io.File you do it like this:
For FTP it's similar; first delete its children, then the folder will be empty and it can be deleted.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
Thanks for this. But this code is useful for single computer i need to delete a directory on remote computer through FTP command.
Here is the code listing i am using to delete a directory


public boolean deleteDirectory(String directory) throws IOException {
if (directory == null)
throw new IllegalArgumentException("argument can not be null");
return connect.sendCmd("RMD " + directory).startsWith(FILE_OPERATION_OK);

}


Where connect is the object of FTPConnection class whose sendCmd method is:


public String sendCmd(String cmd) throws IOException {

if (!isLogin)
return null;
if (cmd.startsWith("LIST") || cmd.startsWith("RETR")
|| cmd.startsWith("APPE") || cmd.startsWith("STOR") || cmd.startsWith("RMD"))
if (isPasv){
datsocket.setActiveSocket();
}
else{
datsocket.setActiveSocket();
}
return ctlsocket.sendCmd(cmd);
}


Again it's call a sendCmd method of FTPControlSocket class which has following code:


protected String sendCmd(String cmd) throws IOException {
String tmp= cmd+"\r\n";

if(isDebug)
debug.log(cmd,Logger.COMMAND);

writer.write(tmp.getBytes(connection.getCharset()));
writer.flush();
return getResponse();
}


can you help me out now.

With Regards,
Pradeep Sharma
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You just need to translate my code for local deletion to code for FTP deletion. The deleting itself is pretty straightforward, now all you need to do is find out how to list the files and sub folders in a specific folder.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to remove remote non-empty directory in FTP