none
How to count files and change directory using FTP/C#? RRS feed

  • Question

  • Hi All,

    I need to connect to an old VMS server, that's not supported, via FTP, and change directory multiple times within the same session, to count the files in each directory.

    I believe I can do this within Wininet.dll <g class="gr_ gr_327 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="327" id="327">api</g> in C/C++.
    https://msdn.microsoft.com/en-us/library/ms909399.aspx?f=255&MSPPError=-2147217396
    FtpSetCurrentDirectory

    But when I look at the .NET Framework, I don't see a change directory function within FTP.

    .NET FrameWork
    https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequestmethods.ftp?view=netframework-4.7.2
    .NET Framework FTP

    Any idea what is the best way to change directory (multiple times) within the same FTP session, and count a total number of files within each directory?

    NOTE: I would also like to count the files in read-only if that's possible, just in case one of the files may be getting processed.

    Thanks.

    Wednesday, February 13, 2019 1:34 AM

Answers

  • Hi mlong219,

    Thank you for posting here.

    According to your description, you want to count files and change directory in ftp.

    In the .NET Framework, we could not find the direct method is similar to the FtpSetCurrentDirectory method.

    However ,you could try the following code to assign the test1 folder to the test2 folder in ftp and count the file number of the directory by using FtpWebRequest.Create method.

    static void Main(string[] args)
            { 
                StringBuilder result = new StringBuilder();
                StringBuilder result1 = new StringBuilder();
                FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(@"ftp://xxx/test2"));
                FtpWebRequest ftpWeb = (FtpWebRequest)FtpWebRequest.Create(new Uri(@"ftp://xxx/test1"));
                ftpWeb.UseBinary = true;
                ftpWeb.Credentials = new NetworkCredential("user", "pass");
                ftpWeb.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse webResponse = ftpWeb.GetResponse();
                StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.Default);
                string line1 = streamReader.ReadLine();
                while (line1 != null)
               {
                    result1.Append(line1);
                    result1.Append("\n");
                    line1 = streamReader.ReadLine();
                }
                result1.Remove(result1.ToString().LastIndexOf('\n'), 1);
                streamReader.Close();
                webResponse.Close();
                string[] m2 = result1.ToString().Split('\n');
                Console.WriteLine("the file number of test1"+"  "+m2.Length);
                foreach (var item in m2)
                {
                    Console.WriteLine(item);
                }
                reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential("user", "pass");
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                    WebResponse response = reqFTP.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    reader.Close();
                    response.Close();
                    string[] m1= result.ToString().Split('\n');
                Console.WriteLine("the file number of test2" + "  " + m1.Length);
                foreach (var item in m1)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
    
    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 14, 2019 7:18 AM
    Moderator

All replies

  • Hi mlong219,

    Thank you for posting here.

    According to your description, you want to count files and change directory in ftp.

    In the .NET Framework, we could not find the direct method is similar to the FtpSetCurrentDirectory method.

    However ,you could try the following code to assign the test1 folder to the test2 folder in ftp and count the file number of the directory by using FtpWebRequest.Create method.

    static void Main(string[] args)
            { 
                StringBuilder result = new StringBuilder();
                StringBuilder result1 = new StringBuilder();
                FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(@"ftp://xxx/test2"));
                FtpWebRequest ftpWeb = (FtpWebRequest)FtpWebRequest.Create(new Uri(@"ftp://xxx/test1"));
                ftpWeb.UseBinary = true;
                ftpWeb.Credentials = new NetworkCredential("user", "pass");
                ftpWeb.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse webResponse = ftpWeb.GetResponse();
                StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.Default);
                string line1 = streamReader.ReadLine();
                while (line1 != null)
               {
                    result1.Append(line1);
                    result1.Append("\n");
                    line1 = streamReader.ReadLine();
                }
                result1.Remove(result1.ToString().LastIndexOf('\n'), 1);
                streamReader.Close();
                webResponse.Close();
                string[] m2 = result1.ToString().Split('\n');
                Console.WriteLine("the file number of test1"+"  "+m2.Length);
                foreach (var item in m2)
                {
                    Console.WriteLine(item);
                }
                reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential("user", "pass");
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                    WebResponse response = reqFTP.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    reader.Close();
                    response.Close();
                    string[] m1= result.ToString().Split('\n');
                Console.WriteLine("the file number of test2" + "  " + m1.Length);
                foreach (var item in m1)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
    
    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 14, 2019 7:18 AM
    Moderator
  • Thanks for the quick and detail response, Jack. But I figured this could be a coding nightmare using .NET, because I have to check about 50+ folders on this legacy server, so I may have to go with the old Win32 API. I need to make one connection, because this old legacy system is very limited on resources, and I can't afford to be making 50+ connections every 30min to the legacy server.

    Thanks!
    Thursday, February 28, 2019 7:42 AM