VBScript - Find text in a txt file using regular expressions and write to another output file

Posted on 2006-05-01
Last Modified: 2008-01-09
Good afternoon,

I am looking for a way in a VBScript to find text based on a regular expression search pattern and write that output to another file (taking into account that the search pattern may need to return more than one result).  I have found a lot of resources on replacing text strings in a txt file which is fairly simple but little on selecting text in a file based on a regular expression, capturing it as a string, and writting this string to another output file.  Any links or code examples would be appreciated.  I am executing this from a batch file with the cscript command line utility.

Question by:orionpm
    LVL 7

    Accepted Solution

    I can give you some guidance anyway and I hope this helps point you in the right direction.

    1) Your VBScript code needs to declare the filesystem object
    2) point the filesystem object in the direction of your text file
    3) read the contents of the text file into a local variable
    4) make the changes using the 'replace' command or grab the text you need using 'instr' and a combination of string manipulation commands (left, mid etc)
    5) write the variables to your new text file using the filesystem object again
    6) close the filesystem object

    Hope this helps!
    LVL 1

    Author Comment

    Thanks, the issue I was having was grabbing the text I read using regular expressions.  After sifting through the Micorosoft documentation on this I am able to pass my regular expression in as an argument from the command line which is working great.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now