Finding and deleting rows in text file programatically

I have many text files with consisting of a records (rows) beginning with specific segment ids.
I have the need to find these rows containing specific segment id's at the beginning of the row.  Once found the line/row needs to be deleted, i.e., not leaving a blank line.

Example:

AAA*HI*AC~
BBB*R*MI*10~
CCC*999*Y*6~
DDD*999*CD*2393939~
EEE*384*DC*340384~
FFF*823*YY*222222~

I want to open each respective text file in a  loop (like VB macro in word) and delete all rows beginning with CCC, DDD and EEE leaving no blank lines.
66chawgerAsked:
Who is Participating?
 
tdlewisCommented:
As long as the strings "CCC", "DDD", and "EEE" followed by an asterisk do not appear in the middle of a row, the following will work:
    Selection.Find.ClearFormatting
    With Selection.Find
        .text = "<[CDE]{3}>\*[!^l-^13]@[^l-^13]"
        .MatchWildcards = True
    End With
    Selection.Find.Execute
    Do While Selection.Find.found
        Selection.Delete
        Selection.Find.Execute
    Loop

Open in new window

0
 
tdlewisCommented:
Do you want a VBA macro to be run from Microsoft Word? If not, what kind of solution did you have in mind?

Is the script supposed to change all *.txt files in a directory? How do you want to specify the directory?
0
 
66chawgerAuthor Commented:
I have a word macro(s) where I can specify the data path and file types to open and save/close at the end.  I just need the code in the  middle once the file is open to do the deletions.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
66chawgerAuthor Commented:
tdlewis, thanks for the example.  Yes, the segment codes are always at the beginning followed by *.  Let me give this a try on a couple of the data files.
0
 
66chawgerAuthor Commented:
Ok, this definitely deleted the data on that line, however, there is a blank line left.  I need to be able to remove the blank line after the data has been deleted, or incorporate that into the delete.  I was playing around with it some, but have not found this command yet.
Please let me know if you have a solution... thanks.
0
 
66chawgerAuthor Commented:
Good info, and concise responses..thanks!
0
 
tdlewisCommented:
This code will replace all instances of two paragraph marks with one paragraph mark:
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^p^p"
        .Replacement.Text = "^p"
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.