Results 1 to 7 of 7

Thread: [2005] Text file Manipulation

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Exclamation [2005] Text file Manipulation

    Hi,

    I have a text file that has this text in one of the lines - "Total Qty".
    I am wondering if it possible to programmatically locate the text and and delete the whole line from the text file.

    thanks

  2. #2
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Re: [2005] Text file Manipulation

    Open the text file into your textbox then replace the text in the textbox and then save it.

    use open file dialog to load the text into your textbox1, use code: textbox1.text = textbox1.text.replace(oldtext,newtext) to replace the text, then use WriteAllText to write it back to what ever file you edited.

    Edit: oldtext = "Total Qty", newtext = ""
    If my post has been helpful, please rate it!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: [2005] Text file Manipulation

    Hello JXDOS,
    what you write looks to be pretty logical; only problem is I am not sure if I can code the same..
    Wondering if someone cane help

    Data in Sample text (c:\test\Data.txt)

    ASDADAD13123123
    ASDASDASD12312323
    ASD123123
    ADHDGD
    ASD4234234
    ASDAAADD
    DA
    DAD
    Total Qty
    dasd
    524234
    sdfsf
    456456
    fasad
    35345345
    asdasdad

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [2005] Text file Manipulation

    Wouldn't you read it in (think this is fairly easy?) and compare against a removal list. Write it to a new file (use the built in file dialogs?) if it is not in the removal list.

    For the future...
    If you use a text file of removal items, you could import this into an array then scan your file and create the new one. This means iti s flexible incase you need to add to the list in the future.

    pseudocode (easy one):
    - open the file to read in
    - open the file to write to
    -- read in a line - hold in a variable
    - compare to the removal list (case statement?)
    - if passes write it to the output file
    -- Loop until no more lines
    - close input file
    - close output file


    Try doing that in code and post up if you have problems.
    You can expand by using a sub with parameters - file to read, file to write

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Text file Manipulation

    vb Code:
    1. Dim myLines As New List(Of String)
    2.  
    3.         Using SR As New IO.StreamReader("c:\test\Data.txt")
    4.             Dim temp As String = Nothing
    5.             Do While SR.Peek <> -1
    6.                 temp = SR.ReadLine
    7.                 If Not temp.Contains("Total Qty") Then
    8.                     myLines.Add(temp)
    9.                 End If
    10.             Loop
    11.         End Using
    12.  
    13.         IO.File.WriteAllLines("c:\test\Data.txt", myLines.ToArray)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Re: [2005] Text file Manipulation

    Well, I started writing this out when I realized that you meant removing the whole line, well, here is how you would get rid of that one word:

    Some appropriate event(probably after file load into the textbox)
    VB.NET Code:
    1. Dim text as string
    2. text = textbox.text.replace("Total Qty","")

    But, to take out a whole line I would do something like this:

    Get your text loaded into a textbox
    Make a dynamic array to hold each line
    VB.NET Code:
    1. Dim Line() as string
    2. Dim Text as string

    Set the entire text into a variable
    VB.NET Code:
    1. text = textbox.text
    Now loop through the entire text and placing each line into the array "line".
    for each line in text

    inside the for loop you could add code to replace the entire line to nothing if it has that word.

    Sorry, but that's all I can remember about how I did it last time I had to code something like that. Good search words would be "Visual Basic text parsing".


    [Code tags added - Mod]

  7. #7
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: [2005] Text file Manipulation

    Iam working on this same function at the moment so i thought id post this example i have found

    http://msdn2.microsoft.com/en-us/lib...1y(VS.80).aspx

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width