Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

Delete line from text file

I am trying to delete a line in a text
Here is the scenario:
there was an error like no connection to the database. so I have a streamwriter write the sql query to a text file - when there is a connection the code checks to see if the file has any lines - if it does it processes it . If the app successfully uploads that line of sql code I want to delete that line from the text file and continue to process the file till it is complete.
mycount is the count of lines in the text file
 if (mycount > 0)
            {
                string line = string.Empty;
                string newline = string.Empty;
                int lineNumber = 0;
                string sqlStr = string.Empty;
                string sqlfile = "\\sqlfile.txt";
               // string sqlfile1 = "\\sqlfilenew.txt";
                bool iWrote = false;
                DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());
                System.IO.StreamReader file = new System.IO.StreamReader(di.ToString() + sqlfile);
                while ((sqlStr = file.ReadLine()) != null)
                {
                    
                    try
                    {
                        iWrote = ss.UploadBackLog(sqlStr);
                        file.Close();
                        if (iWrote)
                        {
                            System.IO.StreamReader cutline = new System.IO.StreamReader(di.ToString() + sqlfile);
                            System.IO.StreamWriter sw = new System.IO.StreamWriter(di.ToString() + sqlfile);

                            while ((line = cutline.ReadLine()) != null)
                            {
                                if ((lineNumber + 1).Equals(lineNumber))
                                {
                                   // 
                                }
                                else
                                {
                                   // sw.WriteLine(line + Environment.NewLine);

                                }
                                lineNumber++;
                            }

                            file.Close();

                            
                        }
                    }
                    catch(Exception ex)
                    {
                        iWrote = false;
                    }
                }

Open in new window

Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

I think the most efficient way to do that is write remaining lines those haven't been processed into a new file, and then rename new file over the old one after reading through all the lines (and properly close the stream for reading old file).

However, if your text file is not that big, I suggest you do read all the lines from text file into memory, process them and save back the lines those haven't been processed. Sample code could be:
string[] allLines = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), "sqlfile.txt"));
for (int i = allLines.Length - 1; i >= 0; i--)
{
     // TODO: process line number i here
     iWrote = ss.UploadBackLog(sqlStr);
     if (iWrote)
     {
           allLines.RemoteAt(i);
     }
}

// finally write remaining lines back to the file
File.WriteAllLines(Path.Combine(Directory.GetCurrentDirectory(), "sqlfile.txt"), allLines);

Open in new window

Avatar of r3nder

ASKER

thanks Duy
I need to process them 1 at a time because the file has the potential to be large - I have done this but can only get it to proccess 1 line  - how can I get it to process all lines
        private void processSQL(int mycount)
        {
            if (mycount > 0)
            {
                string line = string.Empty;
                string newline = string.Empty;
                string sqlStr = string.Empty;
                string sqlfile = "\\sqlfile.txt";
                string sqlfile1 = "\\sqlfilenew.txt";
                bool iWrote = false;
                DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());
                using (var newfile = File.Create(di.ToString() + sqlfile1))
                {
                    //only used this to close the connection to the file after it was created
                }

                System.IO.StreamReader file = new System.IO.StreamReader(di.ToString() + sqlfile);

                while ((sqlStr = file.ReadLine()) != null)
                {
                    try
                    {
                        iWrote = ss.UploadBackLog(sqlStr);
                        file.Close();
                        if (iWrote)
                        {
                            System.IO.StreamReader cutline = new System.IO.StreamReader(di.ToString() + sqlfile);
                            System.IO.StreamWriter sw = new System.IO.StreamWriter(di.ToString() + sqlfile1);
                            while ((line = cutline.ReadLine()) != null)
                            {
                                if (line != sqlStr)
                                {
                                    sw.WriteLine(line);
                                }

                            }
                            cutline.Close();
                            File.Delete(di.ToString() + sqlfile);
                            sw.Flush();
                            sw.Close();
                            File.Move(di.ToString() + sqlfile1, "sqlfile.txt");
                        }
                    }
                    catch (Exception ex)
                    {
                        iWrote = false;
                    }
                }
                file.Close();
            }
        }

Open in new window

Avatar of r3nder

ASKER

its like it goes through the while loop only once
ASKER CERTIFIED SOLUTION
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r3nder

ASKER

awesome Duy Thank you for the help