Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to remove remote non-empty directory in FTP

 
Pradeeep Sharma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeeep Sharma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic