Get a specific file in an FTP directory

1

I have two methods, one sends a file via FTP and the other one consumes a file via FTP.

The files have the dynamic name DateTime.Now each day a new file. example: coletas-04-04-2017 16_00_19.xlsx

In the Send method I get the current date and give a getFile (); this will always return the file of the day, regardless of the time.

string DataAtual = DateTime.Now.ToString("yyyyMMdd");

//pathArquivoConsumido = local onde o arquivo esta que sera enviado;
DirectoryInfo objDiretorio = new DirectoryInfo(pathArquivoConsumido);
FileInfo[] arquivoData = objDiretorio.GetFiles("Coleta_" + DataAtual + "*.csv");

Now in the File consumption method, I would like to do the same thing as the FileInfo[] arquivoData = objDiretorio.GetFiles("Coleta_" + DataAtual + "*.csv"); faz, só que no Diretório FTP

This is the line I use the file, but here I already have to pass the full name,

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pathFull);

So it's impossible to get only the date '

    
asked by anonymous 06.04.2017 / 21:52

1 answer

2

It is necessary to make a request for FTP using the ListDirectoryDetails method, the return of this request will be the name of all files (and directories) separated by a line break. p>

After that, just work with what was returned, for example, make a Split and create an array with the name of all the files and download the ones that are needed. >

Here's an example.

var request = (FtpWebRequest)WebRequest.Create("ftp://www.seusite.com/pasta");  
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;  

request.Credentials = new NetworkCredential("usuario", "senha");    
var response = (FtpWebResponse)request.GetResponse();  

var responseStream = response.GetResponseStream();  
var reader = new StreamReader(responseStream);  
string arquivos = reader.ReadToEnd();

reader.Close();  
response.Close(); 

var arrayArquivos = names.Split(new string[] { "\r\n" }, 
                                    StringSplitOptions.RemoveEmptyEntries).ToList();

foreach(var arquivo in arrayArquivos.Where(a => a.StartsWith($"Coleta_{DataAtual}" 
                                             && a.EndsWith(".csv").ToList())
{
    Download(arquivo);
}
    
06.04.2017 / 22:03