Juan Iriarte
Juan Iriarte

Reputation: 25

How to get files by a partial name c#

I´m doing a program that read sales files and another that read the stock, all in csv extension. Both files are in the same location "C:\LOCAL_DOWNLOAD".

First, the system looks in a table in SQL Server that which state is "Listado-Descargado" after that read the name of the file of the sales, and then modify the csv file to a another format and saving in another table.

I need to do the same work but with the stock files. But, the system reads all the csv files.. including the sales files. Is there any form to read the csv file by its partial name?

Sales files begin with this format: "Poti_L6_2019-06-07" Stock files begin with this format: "Open25_Stock_Date.."

I´ve tried with the method getfiles to get the partial name of the stock files. But that doesnt work.

Also, Ive checked if the streamreader reads the csv file, and it worked, but i need to do the same with all the stock files.

  1) //Sales files
     sqlConnection = new SqlConnection(CNN);
                    command = new SqlCommand();

                    appendLog("699 -  Starting StreamREader " + DateTime.Now.ToString("G")+"\r\n");
                    //using (var reader = new StreamReader(@"C:\666.csv"))
                    appendLog("701 - Iniciando transformacion del csv " + DateTime.Now.ToString("G") + "\r\n");
                    appendLog("702 - Transformacion del csv - "+ @LOCAL_DOWNLOAD + "\\" + ds.Tables[0].Rows[i][0].ToString().Replace(" ", "").Replace("/shared", "") + "\r\n");

                    using (var reader = new StreamReader(@"C:\Proyecto SFTP -SQL-Pedidos_Poti\LOCAL_DOWNLOAD\" + ds.Tables[0].Rows[i][0].ToString().Replace(" ", "").Replace("/shared", "")))

                    {

                        appendLog("705 - Before while " + DateTime.Now.ToString("G") + "\r\n");
                        while (!reader.EndOfStream)

                        {
                            Console.WriteLine("ENTRE AL WHILE");
                            try
                            {
                                appendLog("708 -  Starting StreamREader " + DateTime.Now.ToString("G") + "\r\n");
                                string line = reader.ReadLine();
                                Console.WriteLine("Linea es.. " + line);

//---------------- Then here change the format of the csv and insert in another table.

// Stock Method



 param = new SqlParameter("@SQL", "SELECT Archivo FROM [dbo].[T001_FTP_ESTADO_DOWNLOAD] WHERE [BAJA-SUBA] = 'Listado-Descargado' AND Archivo like '%Open25%'");
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            adapter = new SqlDataAdapter(command);
            adapter.Fill(ds);

            sqlConnection.Close();

            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                try
                {
                    appendLog("949-Selecciono los que empiezen con 
 OPEN25_STOCK         " + DateTime.Now.ToString("G") + " : " + 
  ds.Tables[0].Rows[i][0].ToString().Replace(" ", "") + " successed" + " 
   / Start \r\n");

                    String Fecha_Stock = "";
                    String Sucursal = "";
                    String Cod_Art = "";
                    String Ean = "";
                    String Stock_Actual = "";
                    String Art_En_Promocion = "";
                    String Fecha_Inicio_Promo = "";
                    String Fecha_Fin_Promo = "";


                    Boolean BFecha_Stock;
                    Boolean B_Sucursal;
                    Boolean BCod_Art;
                    Boolean BEan;
                    Boolean BStock_Actual;
                    Boolean BArt_En_Promocion;
                    Boolean BFecha_Inicio_Promo;
                    Boolean BFecha_Fin_Promo;

                    sqlConnection = new SqlConnection(CNN);
                    command = new SqlCommand();


                    string[] directorio = 
 Directory.GetFiles("C:\\Proyecto SFTP -SQL- 
Pedidos_Poti\\LOCAL_DOWNLOAD\\" + "Open25_Stock");
                    string filecontent = string.Empty;
                    string parcial = "Open25_Stock_L6";


                    foreach (string file in directorio)
                    {

                        //string fileContent = reader.ReadToEnd();
StreamReader sr = new StreamReader("C:\\Proyecto SFTP -SQL- 
Pedidos_Poti\\LOCAL_DOWNLOAD\\");

                        string data = sr.ReadLine();
                        string fileContent = sr.ReadToEnd();

                        Console.WriteLine(data);


                            }

Upvotes: 0

Views: 565

Answers (1)

JJJCoder
JJJCoder

Reputation: 16986

You can get a filtered list of paths, by using Directory.GetFiles.

Here is an example:

var stockFiles = System.IO.Directory.GetFiles(fullDirectoryPath, "Open25_Stock*");

Note here that the second parameter is the file name with a wild card for anything after your static text identifier, then pass the file into the StreamReader.

This will give you a string array of the full paths to each of the stock files (excluding the sales files).

For additional clarification (as you're new). I think this code would go at the place of you string[] directorio

Upvotes: 1

Related Questions