none
Read a file, remove line containing word 'bad', write the file to a new file.

    Question

  • I am reading a large text file to a list then searching for the line that contains the word 'bad'. How do I write the file without that line back to a new file?

    Here is my code;

    namespace ReadAndWriteFile
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> theWholeFile = File.ReadLines(@"C:\Users\Desktop\Current Projects\ReadAndWriteFile\ReadAndWriteFile\bin\Debug\01Aug.txt").Reverse().Take(5).ToList();
                foreach (string line in theWholeFile)
                {
                    if ( line.Contains("bad"))
                    {
                        theWholeFile.Remove(line);
                        File.WriteAllLines(@"C:\Users\Public\Success", theWholeFile.ToArray());
                      
                    }
                }
                
                Console.Read();
            }
            
        }
    }



    Tuesday, April 18, 2017 3:52 PM

All replies

  • Try this:

    File.WriteAllLines( @"C:\...\OutputFile.txt", File.ReadLines( @"C:\...\InputFile.txt" ).Where( s => !Regex.IsMatch( s, @"\bbad\b" ) ) );

    It remove all of the lines that contain "bad".

    But if Reverse and Take have some additional meaning, then give more details about the problem.

    • Proposed as answer by CoolDadTxModerator Wednesday, April 19, 2017 2:31 PM
    • Edited by Viorel_MVP Wednesday, April 19, 2017 4:13 PM Spelling.
    Tuesday, April 18, 2017 6:31 PM
  • This is an executable that is opened by Windows Task Scheduler every day.  It searches for 'LINE COUNT' and removes this from each file in an FTP folder.  Just take out the lines that you don't need....

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using System.Threading;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                string folder = @"\\local\FTP\";
                string[] myFiles = Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories);
    
                foreach (var file in myFiles)
                {
                    string dateString = file.Substring(38, 8);
                    DateTime date = DateTime.ParseExact(dateString, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None);
                    TimeSpan ageOfFile = DateTime.Today - date;
                    if (ageOfFile.TotalDays > 30)
                    {
                        //Do your processing here
                        File.Delete(file);
                    }
                }
    
                const int RetryCount = 55;
                int attempts = 0;
    
                while (attempts++ < RetryCount)
    
                    try
                    {
    
                        foreach (var file in Directory.GetFiles(@"\\local\FTP\"))
                        {
                            string[] data = File.ReadLines(file).Where(line => !line.Contains("LINE COUNT")).ToArray();
                            File.WriteAllLines(file, data);
                        }
    
                        break;
                    }
    
                    catch (Exception e)
                    {
                        string lines = (e.ToString());
                        string path = @"C:\log.txt";
                        DateTime time = DateTime.Now;
                        string format = "ddd, MM/dd/yy hh:mm:ss";
    
                        // If the file doesn't exist, create it! 
                        if (!File.Exists(path))
                        {
                            // Create a file to write to. 
    
                            string createText = time.ToString(format) + Environment.NewLine + lines + Environment.NewLine;
                            File.WriteAllText(path, createText);
                        }
    
                        // Assuming the file exists, we will write to the file.
                        string appendText = time.ToString(format) + Environment.NewLine + lines + Environment.NewLine;
                        File.AppendAllText(path, appendText);
    
                        // Wait 1 minute and re-try!!
                        var stopwatch = Stopwatch.StartNew();
                        Thread.Sleep(60000);
                        stopwatch.Stop();
                   }
                }
            }
        }
    


    MY BOOK


    • Edited by ryguy72 Wednesday, April 19, 2017 1:36 AM
    Wednesday, April 19, 2017 1:35 AM
  • Thank you very much Viorel_, what I tried to do in an entire function you have achieved in a single line of code.
    Wednesday, January 16, 2019 4:59 PM