Question

Extract Specific Lines from a rtf file and create a new file

Asked by: cbones

Hello All,

I am trying to create a script that will extract specific lines from a file based on a specific phrase.  I was trying to do it in powershell but it doesn't seem to be working out:
get-Content c:\Users\dan\stephanie\test.rtf | where { $_ -like "1450" }|
add-Content -path c:\Users\dan\stephanie\mantello.rtf

Can it be done in vb or a batch file?

Thank you.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-06-18 at 09:31:18ID24503174
Topics

VB Script

,

Windows Batch Scripting

Participating Experts
2
Points
0
Comments
32

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. RTF TO TEXT
    Is there any program/utility/function that can convert a RTF document into a text file? Thx
  2. extracting rtf (picture)from Notes into VB
    Im trying to create help files from a notes database. one of the fields in the form is a picture(rtf format) it seems that the rtf is broken up into a number of chunks (about 10250 bytes) Using the com call getItem this returns nothing, notes documentation seems to think it ...
  3. Reading RTF documents in VB
    Hello, How can I open and read RTF document line by line in VB6.0. 1) I need to read each line and see if the first or the second word of that line is 'Subject' and if so, extract that line and line number and write to an output RTF file 2) Then I need to scan this RTF d...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Shift-3Posted on 2009-06-18 at 10:00:15ID: 24659396

Paste the script below into a text file with a .vbs extension.  Customize the value of the strInput variable on line 5 with the location of the input file.  Customize the value of the strOutput variable on line 6 with the location of the output file.  Customize the contents of the arrToFind array on line 7 with the string(s) to find.

Running the script will write any matching lines to the output file.

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
strInput = "c:\Users\dan\stephanie\test.rtf"
strOutput = "c:\Users\dan\stephanie\mantello.rtf"
arrToFind = Array("1450")
 
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInput, ForReading, False, TriStateUseDefault)
arrLines = Split(objInput.ReadAll, vbCrLf)
objInput.Close
 
Set objOutput = objFSO.OpenTextFile(strOutput, ForWriting, True)
 
For Each strLine in arrLines
    For Each strToFind in arrToFind
        If InStr(strLine, strToFind) > 0 Then
            objOutput.WriteLine strLine
        End If
    Next
Next
 
objOutput.Close

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:

Select allOpen in new window

 

by: cbonesPosted on 2009-06-18 at 10:56:26ID: 24660018

Hello,

Thank you for posting back to me.  When I run the script I get the same results from when I run the powershell script.  For some reason it is copying over the whole document.  It makes a copy of the document and creates a new file.  

Any ideas?

Thank you.

 

by: Shift-3Posted on 2009-06-18 at 12:01:31ID: 24660683

Can you post a representative sample of the document (with any sensitive information redacted) using the Attach File box?

 

by: cbonesPosted on 2009-06-18 at 13:43:25ID: 24661573

Here is a sample of what the file looks like in word:

7159  test,test 5909  Name  06/01/2009 88888 information #A022947 DD:DD 05/29/2009
4009 test2,test2 1979  Name 06/01/2009 7777  information   DD:DD 05/27/2009
246842 test3,test D606  Name 06/01/2009 6666  information DD:DD 05/26/2009
297174 test4,test4 1450  Name 06/01/2009 55555 information AT:AT 06/01/2009
100044 test5,test5 C940  Name 6/01/2009 70544  information DD:DD: 06/01/2009

There is more data to the file.  I replaced the information on the lines. But it is a sample.

Thank you.

 

by: Shift-3Posted on 2009-06-18 at 14:20:50ID: 24661815

Can you save the file as plain text rather than rtf?

 

by: t0t0Posted on 2009-06-18 at 15:10:57ID: 24662217

How about using a DOS command as in:

    FINDSTR "1450" "c:\Users\dan\stephanie\test.rtf" >>"c:\Users\dan\stephanie\mantello.rtf"

Type: FINDSTR /? in DOS for further options.

 

by: cbonesPosted on 2009-06-18 at 15:11:13ID: 24662220

Yes, I can save the file as a .txt file.

 

by: t0t0Posted on 2009-06-18 at 15:43:07ID: 24662425

Just wondering, if the FINDSTR command worked....

 

by: cbonesPosted on 2009-06-18 at 16:19:58ID: 24662677

FINDSTR doesn't look like it will give me an option to move those lines to another file.  When I use the FINDSTR command, it does not return what I am looking for.

 

by: t0t0Posted on 2009-06-18 at 16:41:36ID: 24662766

Please don't insult me. Instead of saying "doesn't look like it will give me an option to move those lines to another file" why don't you try it? Here is the command again:

    FINDSTR "1450" "c:\Users\dan\stephanie\test.rtf" >>"c:\Users\dan\stephanie\mantello.rtf"

This will search through the file TEST.RTF for any lines containing the text "1450" and if it finds any, IT WILL COPY those lines to the file MANTELLO.RTF (providing of course the folder names you gave are correct).

 

by: cbonesPosted on 2009-06-18 at 16:47:52ID: 24662794

t0t0,

Wasn't trying to insult you.  Didn't mean anything by it.  I did try it before I posted, it returns me the whole file instead of just the criteria I am looking for.  

 

by: t0t0Posted on 2009-06-18 at 17:06:43ID: 24662865

Sorry, I disagree with you. The code I gave you will ONLY copy lines that contain the matching text. The format of the command I gave you is:

    FINDSTR [Text]  [Source File]  >>  [Destination File]

If it helps, mavigate to the folder which contains your source file (test.rtf) and then type this shorter version of the command:

    FINDSTR "1450" test.rtf >mantello.rtf

Now look at the file MANTELLO.RTF and confirm what you see by typing:

    TYPE MANTELLO.RTF

Is it possible you were typing:

   FINDSTR "1450" "c:\Users\dan\stephanie\test.rtf" >>"c:\Users\dan\stephanie\mantello.rtf"

instead of just:

    FINDSTR "1450" "c:\Users\dan\stephanie\test.rtf" >"c:\Users\dan\stephanie\mantello.rtf"

In which case you may have been 'appending' the output to MANTELLO.RTF rather tha creating it?

Please confirm.

 

by: cbonesPosted on 2009-06-18 at 17:17:50ID: 24662919

t0t0,

I typed the command in correctly.  It gives me back a copy of the entire file.  

Thank you.

 

by: t0t0Posted on 2009-06-18 at 17:22:44ID: 24662940

does every line in your source file contains the text: "1450"?

 

by: cbonesPosted on 2009-06-18 at 17:47:58ID: 24663043

No, out of 5 pages of lines, there are only a hand full of lines that contain 1450.  

 

by: t0t0Posted on 2009-06-19 at 02:07:03ID: 24664792

Please run this batch file to confirm the operation of the FINDSTR command:

@ECHO OFF
REM ----------------------------------------------------
REM Set SearchText to '1450' (default) or command line option
REM ----------------------------------------------------
SET SearchText=1450
IF NOT "%1"=="" SET SearchText=%1


REM ----------------------------------------------------
REM Reset files
REM ----------------------------------------------------
DEL 50URCE.RTF 2>NUL
DEL DE5T1NAT10N.RTF 2>NUL


REM ----------------------------------------------------
REM Display notes
REM ----------------------------------------------------
CLS
ECHO.
ECHO This program creates a file named 50URCE.RTF containing the 5 lines you
ECHO supplied to EE question (the first line is truncated to fit on the screen).
ECHO.
ECHO  7159  test,test 5909  Name  06/01/2009 88888 information #A022947 DD:DD 05/2
ECHO  4009 test2,test2 1979  Name 06/01/2009 7777  information   DD:DD 05/27/2009
ECHO  246842 test3,test D606  Name 06/01/2009 6666  information DD:DD 05/26/2009
ECHO  297174 test4,test4 1450  Name 06/01/2009 55555 information AT:AT 06/01/2009
ECHO  100044 test5,test5 C940  Name 6/01/2009 70544  information DD:DD: 06/01/2009
ECHO.
ECHO Then, the FINDSTR command is executed searching for any lines containing the
ECHO text "%SearchText%". These lines are then written to a file named DE5T1NAT10N.RTF.
ECHO.
ECHO Both filenames 50URCE.RTF and DE5T1NAT10N.RTF have the numbers 0 1 and 5
ECHO for the letters o i and s so they are unlikely to effect files on your PC.
ECHO.
PAUSE


REM ----------------------------------------------------
REM Create source file
REM ----------------------------------------------------
ECHO.
ECHO Creating source 50URCE.RTF file...
 >50URCE.RTF ECHO 7159  test,test 5909  Name  06/01/2009 88888 information #A022947 DD:DD 05/2
>>50URCE.RTF ECHO 4009 test2,test2 1979  Name 06/01/2009 7777  information   DD:DD 05/27/2009
>>50URCE.RTF ECHO 246842 test3,test D606  Name 06/01/2009 6666  information DD:DD 05/26/2009
>>50URCE.RTF ECHO 297174 test4,test4 1450  Name 06/01/2009 55555 information AT:AT 06/01/2009
>>50URCE.RTF ECHO 100044 test5,test5 C940  Name 6/01/2009 70544  information DD:DD: 06/01/2009
ECHO.


REM ----------------------------------------------------
REM Check if source file was created
REM ----------------------------------------------------
IF EXIST 50URCE.RTF (
   ECHO The 50URCE.RTF file was successfully created.
) ELSE (
   ECHO Error creating 50URCE.RTF file & EXIT /B
)
ECHO.
PAUSE


REM ----------------------------------------------------
REM Display contents of source file
REM ----------------------------------------------------
ECHO.
ECHO Contents of 50URCE.RTF file
ECHO -----------------------------------
MORE <50URCE.RTF
ECHO -----------------------------------
ECHO.
PAUSE


REM ----------------------------------------------------
REM Execute DOS command
REM ----------------------------------------------------
ECHO.
ECHO Executing the command: FINDSTR "%SearchText%" 50URCE.RTF ^> DE5T1NAT10N.RTF...
ECHO.
ECHO Searching 50URCE.RTF file for "%SearchText%"
ECHO.
FINDSTR "%SearchText%" 50URCE.RTF >DE5T1NAT10N.RTF

IF %ERRORLEVEL% EQU 0 (
   SET SearchResult=Found
) ELSE (
   SET SearchResult=NotFound
   COPY NUL >DE5T1NAT10N.RTF 1>NUL
)
ECHO Finished searchig file.
ECHO.

IF %SearchResult%==Found (
   ECHO The text "%SearchText%" was found in 50URCE.RTF file
   ECHO.
   ECHO Line/s have been written to DE5T1NAT10N.RTF file
) ELSE (
   ECHO The text "%SearchText%" was not found in 50URCE.RTF file
)
ECHO.
PAUSE

REM ----------------------------------------------------
REM Check file was created ok
REM ----------------------------------------------------
IF NOT EXIST DE5T1NAT10N.RTF (
   ECHO Error creating DE5T1NAT10N.RTF file
   EXIT /B
)


REM ----------------------------------------------------
REM Display contents of destination file
REM ----------------------------------------------------
ECHO.
ECHO Contents of DE5T1NAT10N.RTF file
ECHO -----------------------------------
MORE <DE5T1NAT10N.RTF
ECHO -----------------------------------
ECHO.
PAUSE

REM ----------------------------------------------------
REM Clean up
REM ----------------------------------------------------
ECHO.
ECHO Reseting variables...
SET Search=
ECHO.

ECHO Deleting 50URCE.RTF and DE5T1NAT10N.RTF files...
ECHO.
DEL 50URCE.RTF 2>NUL
DEL DE5T1NAT10N.RTF 2>NUL

REM ----------------------------------------------------
REM Check files were deleted ok
REM ----------------------------------------------------
IF EXIST 50URCE.RTF ECHO Error: Cannot delete S0URCE.RTF file
IF EXIST DE5T1NAT10N.RTF ECHO Error: Cannot delete DE5T1NAT10N.RTF file

REM ----------------------------------------------------
REM End
REM ----------------------------------------------------
ECHO End of program.
ECHO.

 

by: cbonesPosted on 2009-06-19 at 08:53:50ID: 24667493

When I run that, it gives back the one entry that has the 1450 in it.   When I try to manipulate it to scan through the file I have, I end up getting back a copy of the entire file.

 

by: t0t0Posted on 2009-06-19 at 09:37:04ID: 24667895

Hmmm.... that's interesting. So, you can see the problem then...

Please, if the data is not sensitive in one of your RTF files, attach one of them here to enable me to investigate why it works for some files and why it doesn't for others.

 

by: cbonesPosted on 2009-06-19 at 11:03:43ID: 24668623

I had to change it to a .txt file to upload it.  You can rename it to .rtf so the format is normal.

Thank you.

 

by: cbonesPosted on 2009-06-19 at 11:05:51ID: 24668641

Not sure if the upload went through.  It does not allow you to upload an .rtf file to the website.

 

by: t0t0Posted on 2009-06-19 at 11:08:11ID: 24668665

Nope, there's no upload here... How about putting it into a ZIP file?

 

by: t0t0Posted on 2009-06-20 at 12:29:16ID: 24674887

I've successfully tried this with my own RTF files. I really need to have a copy of one of your RTF files so that I can investigate why it won't work with yours.

 

by: cbonesPosted on 2009-06-24 at 13:05:54ID: 24705203

I try to upload the file but it doesn't take. Here is the file as a .txt.  I use Microsoft Word to open it.  It sorts out the columns.

Thank you.

 

by: t0t0Posted on 2009-06-25 at 04:24:22ID: 24710184

will report back later today!

 

by: t0t0Posted on 2009-06-27 at 05:10:58ID: 24727396

Regarding the lines that are written out to a new file, do you want this in plain text format containing just the text as it appears in each column or do you want the file to be formatted like the original file with the page header and column names etc.

 

by: cbonesPosted on 2009-06-27 at 08:38:28ID: 24728105

If the file can contain just the text as it appears in each column that would be great.  I don't need the headers.

Thank you!!

 

by: cbonesPosted on 2009-06-30 at 16:56:30ID: 24750696

Hello,

Any luck with the file?

Thank you.

 

by: t0t0Posted on 2009-06-30 at 17:15:47ID: 24750780

I will come back to you on this one very shortly.

 

by: cbonesPosted on 2009-07-08 at 13:43:32ID: 24808145

Hey,

Just wanted to check and see if this was possible.

Thank you.

 

by: t0t0Posted on 2009-09-07 at 14:17:41ID: 25277315

Please tend to this question.

Thank you.

 

by: cbonesPosted on 2009-09-23 at 07:50:12ID: 25403743

I am still not able to extract the lines from the file.  Sorry

 

by: cbonesPosted on 2009-10-12 at 13:28:18ID: 25554710

Close

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...