tags:

views:

45

answers:

2

I'm creating a program where I need to search an FTP server and download all files which match a given regex. How do I do this? I can connect to the FTP server, but how do I scan all files in the given path for files matching the regex?

I also need to do the same for HTTP servers which I think will be fundamentally more difficult, but I'll stick to doing the FTP server for now.

Thanks

A: 

FTP has a list (ls) command - any library you use should have a corresponding method, this will return a listing of files in the current directory.

You can match against this list and only retrieve the files that match.

Without knowing the exact library you are using, it is difficult to get more specific.

Oded
+3  A: 

You can use this to get a list

public string[] GetFileList()
    {
        string[] downloadFiles;
        StringBuilder result = new StringBuilder();
        FtpWebRequest reqFTP;
        try
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
            WebResponse response = reqFTP.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());

            string line = reader.ReadLine();
            while (line != null)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            // to remove the trailing '\n'
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
            reader.Close();
            response.Close();
            return result.ToString().Split('\n');
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            downloadFiles = null;
            return downloadFiles;
        }
    }

and then maipulate GetFileList Array using regex as per your need

Aamod Thakur
Fantastic, thanks :)
Chris