How to delete all txt files inside an FTP directory?

Igor Mytyuk :

My app stores txt files on a FTP server which are also hosted on webservice.

In the directory where I host txt files I can find other txt files. I would like to delete all files in current directory every time when I store the new ones.

Actually i was trying to use the following command:

FTPClient ftpClient = new FTPClient();
ftpClient.connect(siteFTP);
if (ftpClient.login(usrFTP, pswFTP)) {
  ftpClient.enterLocalPassiveMode();          
  FTPFile[] remoteFiles = ftpClient.listFiles(path);
  if (remoteFiles.length > 0) {
    ftpClient.deleteFile("/prenotazioni/*.txt");
  }
}

But even if there was txt files in that directory, FTP respose is:

> DELE /prenotazioni/*.txt
> 550 File not found
deadfish :

Using * won't work. After You get list of files in declared directory, You must iterate it and delete files one by one by using deleteFile(String pathname) (also checking if file name endsWith(".txt")).

Every FTPFile has method getName(). You should construct full path so FTPClient will know what file to delete. I believe it would be something like:

ftpClient.deleteFile("/prenotazioni/" + remoteFiles[i].getName());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135044&siteId=1