none
Delete data from a text file

    Question

  • hello guys ..

    i have a text file ,, and thre are recoreds(as lines) in that file ,,, i want a code to find that recored or data and then delete that line ...

    i tryed but i couldn't fine the right code ...


    thanx for advance ....
    Monday, September 15, 2008 1:32 PM

Answers

  • There are many ways that you can do this, but if you are just looking at reading it in, and writing it out, removing any lines that match ",,," you could do something similar to the following.  (Please note, this might not be 100% correct as I am writing this from scratch, double check the capitalization on the "Peek", "ReadLine", and "WriteLine" methods.


    using(StreamReader oReader = new StreamReader("inputFile.txt"))  
    {  
        using(StreamWriter oWriter = new StreamWriter("outputFile.txt"))  
        {  
            while(oReader.Peek <> -1)  
            {  
                string currentLine = oReader.ReadLine();  
                if(!currentLine.Equals(",,,"))  
                {  
                    oWriter.WriteLine(currentLine);  
                }  
            }  
        }  

    Mitchel Sellers, MCITP, MCPD, MCTS - http://www.mitchelsellers.com
    • Proposed as answer by Mitchel SellersMVP Monday, September 15, 2008 2:05 PM
    • Marked as answer by jack 321 Thursday, September 18, 2008 5:42 AM
    Monday, September 15, 2008 2:05 PM

All replies

  • There are many ways that you can do this, but if you are just looking at reading it in, and writing it out, removing any lines that match ",,," you could do something similar to the following.  (Please note, this might not be 100% correct as I am writing this from scratch, double check the capitalization on the "Peek", "ReadLine", and "WriteLine" methods.


    using(StreamReader oReader = new StreamReader("inputFile.txt"))  
    {  
        using(StreamWriter oWriter = new StreamWriter("outputFile.txt"))  
        {  
            while(oReader.Peek <> -1)  
            {  
                string currentLine = oReader.ReadLine();  
                if(!currentLine.Equals(",,,"))  
                {  
                    oWriter.WriteLine(currentLine);  
                }  
            }  
        }  

    Mitchel Sellers, MCITP, MCPD, MCTS - http://www.mitchelsellers.com
    • Proposed as answer by Mitchel SellersMVP Monday, September 15, 2008 2:05 PM
    • Marked as answer by jack 321 Thursday, September 18, 2008 5:42 AM
    Monday, September 15, 2008 2:05 PM
  • thanx for ur answer , but i want the action in 1 file . like fine the right line then delete it .. on the same file ..

    so here is the problem for me ... doing it in the same file ...

    any help??


    thanx ..
    • Edited by mister_all Monday, September 15, 2008 2:38 PM
    Monday, September 15, 2008 2:12 PM
  • In that case you would have to work on a method to either modify this process to store the input, then re-write the file.  Or after closing out the files, copy the outfile.txt over the infile.txt then delete outfile.txt.

    There might be other ways of doing it, but this method is straight forward, and works with very large files easily.  I have used this process to clean data out of 5+gb text files without issue and very little memory usage.
    Mitchel Sellers, MCITP, MCPD, MCTS - http://www.mitchelsellers.com
    Monday, September 15, 2008 3:58 PM