Ad

How To Delete All Txt Files Inside An FTP Directory?

- 1 answer

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
Ad

Answer

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());
Ad
source: stackoverflow.com
Ad