-3

Is there any way to count number of files present in FTP directory using C# CODE. I do not have access to use SSIS and hence I have to accomplish this task using Visual Studio.

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
rahul
  • 59
  • 2
  • 10
  • You need all the files present in a directory? Refer this- http://stackoverflow.com/questions/14877237/getting-all-file-names-from-a-folder-using-c-sharp – Souvik Ghosh Jan 04 '17 at 05:56
  • I dont wan the file name..I just need the count of files. – rahul Jan 04 '17 at 05:58
  • 2
    The text 'C# ftp count files' plugged into google yields a complete tutorial as the first result. Please do research before posting questions like this. Yes, of course it's possible to count the number of files in FTP. Do some research. – Rob Jan 04 '17 at 05:59
  • Did you Google this? It's pretty common. http://stackoverflow.com/questions/2242564/file-count-from-a-folder – Souvik Ghosh Jan 04 '17 at 06:00
  • @rob,@souvik-my primary task is to get number of files from FTP location and email the same to the user.How do i do it..?? – rahul Jan 04 '17 at 06:05

1 Answers1

2

Use the following code,

public static String[] FTPListTree(String FtpUri, String User, String Pass) {

List<String> files = new List<String>();
Queue<String> folders = new Queue<String>();
folders.Enqueue(FtpUri);

while (folders.Count > 0) {
    String fld = folders.Dequeue();
    List<String> newFiles = new List<String>();

    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(fld);
    ftp.Credentials = new NetworkCredential(User, Pass);
    ftp.UsePassive = false;
    ftp.Method = WebRequestMethods.Ftp.ListDirectory;
    using (StreamReader resp = new StreamReader(ftp.GetResponse().GetResponseStream())) {
        String line = resp.ReadLine();
        while (line != null) {
            newFiles.Add(line.Trim());
            line = resp.ReadLine();
        }
    }

    ftp = (FtpWebRequest)FtpWebRequest.Create(fld);
    ftp.Credentials = new NetworkCredential(User, Pass);
    ftp.UsePassive = false;
    ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    using (StreamReader resp = new StreamReader(ftp.GetResponse().GetResponseStream())) {
        String line = resp.ReadLine();
        while (line != null) {
            if (line.Trim().ToLower().StartsWith("d") || line.Contains(" <DIR> ")) {
                String dir = newFiles.First(x => line.EndsWith(x));
                newFiles.Remove(dir);
                folders.Enqueue(fld + dir + "/");
            }
            line = resp.ReadLine();
        }
    }
    files.AddRange(from f in newFiles select fld + f);
}
return files.ToArray();
}

You can also use the followning code:

   List<string> strList = new List<string>();

  FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP));
  fwr.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  fwr.Method = WebRequestMethods.Ftp.ListDirectory;

    //Can object of type StreamReader as given below
  StreamReader sr = new StreamReader(fwr.GetResponse().GetResponseStream());  
  string str = sr.ReadLine();
        while (str != null)
        {
            strList.Add(str);
            str = sr.ReadLine();
        }
        Console.WriteLine(strList.Count);
Dot_NET Pro
  • 2,095
  • 2
  • 21
  • 38