0

When listing files and it's subfiles within the directory in the FTP server here it seems to work when using the recursion call. Here I do it with recursion calling ListFolder(ftpClient, remoteFilePath) and it works, I have to try to do this without recursion. Is there any way to do this, any suggestions on how I can improve this code so I can do this without using recursion

private static void ListFolder(FTPClient ftpClient, String filesDirectory)
    throws IOException
{
    System.out.println("Listing folder " + filesDirectory);
    FTPFile[] remoteFiles = ftpClient.listFiles(filesDirectory);
    for (FTPFile remoteFile : remoteFiles)
    {
        if (!remoteFile.getName().equals(".") &&
            !remoteFile.getName().equals(".."))
        {
            String remoteFilePath = filesDirectory + "/" + remoteFile.getName();

            if (remoteFile.isDirectory())
            {
                ListFolder(ftpClient, remoteFilePath);
            }
            else
            {
                System.out.println("Found file " + remoteFilePath);
            }
        
        File downloadFile1 = new File(directory + "/" + remoteFile);
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile1));
          ftpClient.retrieveFile(remoteFilePath, outputStream);
    }
}
Ajm Kir
  • 17
  • 4
  • 1
    Is there any reason that recursion does not work for you? Otherwise see here for a solution: [Ideas for listing files, directories, sub files & sub directories without recursion in ftp server](https://stackoverflow.com/questions/66515441/ideas-for-listing-files-directories-sub-files-sub-directories-without-recurs) – sorifiend Jul 05 '22 at 00:52
  • 1
    If you are trying to get the directory structure and all files in a single call then see here: [Get a whole FTP directory listings recursively in one call possible to reduce time](https://stackoverflow.com/questions/72793174/get-a-whole-ftp-directory-listings-recursively-in-one-call-possible-to-reduce-ti) – sorifiend Jul 05 '22 at 00:56
  • It works but I believe I have to try to do it without recursion – Ajm Kir Jul 05 '22 at 13:30

0 Answers0