0

On my FTP Server I have the following folder structure

 - Parent Directory
   -a.txt
   -b.txt.old
   -SubDirectory1
     -c.txt
     -NestedSubDirectory1
       -d.txt
   -SubDirectory2
     -e.txt
     -f.txt.old

The number of SDs are not fixed. I need a way to get all the files(can be any format) without the .old extension from the Parent Directory. I'm currently using the 3rd party dll edtFTPnet.

ftpConnection.GetFileInfos()Where(f => !(f.Name.EndsWith(".old"))).ToList();

This helps me get the details of the files and folders at the current working directory level.

Can someone tell me a way to get all the files with the parentdirectory, subdirectories and nested subdirectories.

The solution may or may not use edtFTPnet.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Jovel JV
  • 11
  • 1
  • 4

2 Answers2

2

FTPConnection.GetFileInfos() returns an array of FTPFile. The class FTPFile has a boolean property Dir which indicates whether its filename accesses a file (false) or directory (true).

Something like this should work:

void ReadSubDirectories(FTPConncetion connection, FTPFile[] files)
{
    foreach (var file in files)
    {
        if (file.Dir)
        {
            // Save parent directory
            var curDir = connection.ServerDirectory;

            // Move into directory
            connection.ChangeWorkingDirectory(file.Name)

            // Read all files
            ReadSubDirectories(connection, connection.GetFileInfos());

            // Move back into parent directory
            connection.ChangeWorkingDirectory(curDir)
        }
        else
        {
            // Do magic with your files
        }
    }
}

However you might be better off using just .NET's built-in FtpWebRequest class since its methods and naming conventions are clearer, it's better documented and it's easier to find references online.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Hey, thanks a lot..I did check out FtpWebRequest initially, but surprisingly did not find too many references/documentation on it. Could you please provide few links to it. Also I found dealing with ftpfiles or other data types to be easier than dealing with stream data returned by FTPResponse.... – Jovel JV Oct 30 '12 at 11:21
0

Try to use extensions like this:

class Program
    {
        static void Main(string[] args)
        {
            using (var connection = new FTPConnection
            {
                ServerAddress = "127.0.0.1",
                UserName = "Admin",
                Password = "1",
            })
            {


                connection.Connect();
                connection.ServerDirectory = "/recursive_folder";
                var resultRecursive =
                    connection.GetFileInfosRecursive().Where(f => !(f.Name.EndsWith(".old"))).ToList();
                var resultDefault = connection.GetFileInfos().Where(f => !(f.Name.EndsWith(".old"))).ToList();


            }

        }
    }

    public static class FtpClientExtensions
    {
        public static FTPFile[] GetFileInfosRecursive(this FTPConnection connection)
        {
            var resultList = new List<FTPFile>();
            var fileInfos = connection.GetFileInfos();
            resultList.AddRange(fileInfos);
            foreach (var fileInfo in fileInfos)
            {
                if (fileInfo.Dir)
                {
                    connection.ServerDirectory = fileInfo.Path;
                    resultList.AddRange(connection.GetFileInfosRecursive());
                }
            }
            return resultList.ToArray();
        }
    }
testCoder
  • 7,155
  • 13
  • 56
  • 75