
Questions: 23,724 //
Answers: 52,294 //
Contributing Members: 17,991
Hi Guys,
Currently I'm uploading some files to a SFTP Server, every day I create a folder in this format (ddMMYY). Eventually I'm going to need to remove some of those folders and just leave the current one.
Is there a way to delete the last folder created or delete a folder based on the name from the SFTP server?
What would be the right approach?
Thanks.
Jul 11, 2017 at 04:51 PM, deepakmantry1 answered with:
It seems the functionality is not possible through available options in SFTP endpoint. However you may use direct java call for this.
Refer below link for details. http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/transport/sftp/SftpClient.html#deleteDirectory(java.lang.String)
Thanks Deepak
Awesome!, I will try it and I will share the result. Thanks for the response.
Jul 12, 2017 at 11:46 AM, emoran answered with:
I just wrote this java script. it's working fine.
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import org.mule.transport.sftp.SftpClient;
public class STSFTPController extends AbstractMessageTransformer{
@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
Boolean result=false;
STSFTPController controller = new STSFTPController();
String sftp_host = (String)message.getInvocationProperty("sftp_host");
sftp_host = sftp_host.replaceAll("\\s+","");
String userName = (String)message.getInvocationProperty("sftp_username");
userName = userName.replaceAll("\\s+","");
String password = (String)message.getInvocationProperty("sftp_password");
password = password.replaceAll("\\s+","");
SftpClient client = new SftpClient(sftp_host);
try{
client.login(userName,password);
logger.info("SFTP Client connected correctly.");
result = controller.deleteDirectory(message.getInvocationProperty("yesterdayFolderPath"),client);
logger.info("SFTP Folder Removed: ."+result);
}
catch(Exception er){
logger.info("Error occurred: "+er.getMessage());
}
return result;
}
/**
*
* @param path
* @param client
* @return
*/
public Boolean deleteDirectory(String path,SftpClient client){
Boolean deleted;
try{
client.deleteDirectory(path);
deleted = true;
}
catch(Exception er){
logger.info("Error occurred: "+er.getMessage());
deleted = false;
}
return deleted;
}
}
Thanks.
Great job , Good to know that it is working . The code snippet can be a reusable snippet to perform additional operation on SFTP connector.