Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys :

the following method is to get certain files and will write them into 1 single file

This is my code :


string[] inputFilePaths = Directory.GetFiles("//path to directory", "*.txt.2018-" + DateTime.Now.ToString("MM") + "-*");


           using (StreamWriter outputStream = new StreamWriter("path to output file" + DateTime.Now.ToString("MM-yyyy") + "-Statistics.txt",false))
           {
               foreach (var inputFilePath in inputFilePaths)
               {
                   using (var inputStream = new StreamReader(inputFilePath))
                   {
                      outputStream.Write(inputStream.ReadToEnd());
                   }

               }
           }


In the directory i have the following files :

TodayLog.txt x
TodayLog.txt.2018-08-08 x
TodayLog.txt.2018-08-09 x
TodayLog.txt.2018-08-10 x
TodayLog.txt.2018-07-29


I need only files indicated with an 'x'. How can i do that pleasE?

What I have tried:

var file = Directory.GetFiles("//path to directory").Where(s => s.Equals("TodayLog.txt") || s.Contains(".txt.2018 - " + DateTime.Now.ToString("MM") + "-"));


I tried this method but it is not returning anything and i need the return as a string[].

Someone can help me please?
Posted
Updated 13-Aug-18 0:37am
v2
Comments
F-ES Sitecore 13-Aug-18 6:23am    
Your file is called

TodayLog.txt.2018-08-08

Use the debugger to look at what the following evaluates to

".txt.2018 - " + DateTime.Now.ToString("MM") + "-"

Compare that literally with your filename to see if you can spot any differences (PROTIP: spaces are characters too, comparison operations don't ignore them)
Joe Doe234 13-Aug-18 6:29am    
the main problem is that i need to get TodayLog.txt as well. I already getting the paths of that files that ends with a date

1 solution

Write a method to process the path:
C#
private bool IsThisMonth(DateTime now, string path)
    {
    DateTime dt;
    if (!DateTime.TryParseExact(Path.GetExtension(path),
                                ".yyyy-MM-dd",
                                CultureInfo.InvariantCulture,
                                DateTimeStyles.None,
                                out dt))
        {
        return false;
        }
    return dt.Year == now.Year && dt.Month == now.Month;
    }
Then it's trivial:
C#
DateTime now = DateTime.Now;
string[] thisMonth = inputFilePaths.Where(f => IsThisMonth(now, f)).ToArray();
 
Share this answer
 
Comments
Joe Doe234 13-Aug-18 7:32am    
sorry but you confused me a little , as far as i am understanding im not getting the file TodayLog.txt but with this method right ?
OriginalGriff 13-Aug-18 7:40am    
Change
return false;
to
return true;
and it will return this month, and everything that doesn't end in a date.
Joe Doe234 13-Aug-18 7:46am    
i am debugging it and changed the return to true but the method is still not returning todaylog.txt with other textfiles for this month
OriginalGriff 13-Aug-18 7:56am    
Is it in the original list?
Joe Doe234 13-Aug-18 8:06am    
yes it is in the same directory

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900