Solved

How to Search a key word and add a text at the end of line using VBS??

Posted on 2015-02-11
9
54 Views
Last Modified: 2015-02-12
I need a VBS script to search the key word  "B" and once found then go to end of line and this comment: #This line is special

For more info you can see the attachment.
img.jpg
0
Comment
Question by:acunaara
  • 5
  • 4
9 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40605629
please post a text file with the source data to be changed
0
 

Author Comment

by:acunaara
ID: 40605802
this is the file
file.txt
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40605964
I named your file Q_28615458.txt
Sub Q_28615458()
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim oFS, oTS
    Dim strFileData
    Dim oRE
    
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oTS = oFS.OpenTextFile("c:\users\aikimark\downloads\Q_28615458.txt", ForReading, False)
    strFileData = oTS.readall()
    oTS.Close

    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = True
    oRE.Pattern = "(""B"".*)(\r\n)"

    If oRE.test(strFileData) Then
        strFileData = oRE.Replace(strFileData, "$1" & vbTab & "#This line is special$2")
    End If

    Set oTS = oFS.OpenTextFile("c:\users\aikimark\downloads\Q_28615458.txt", ForWriting, False)
    oTS.write strFileData
    oTS.Close
End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:acunaara
ID: 40606256
mmm It does not work
0
 

Author Comment

by:acunaara
ID: 40606259
even I rename my file and its path

Sub Q_28615458()
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim oFS, oTS
    Dim strFileData
    Dim oRE
    
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oTS = oFS.OpenTextFile("C:\Q_28615458.txt", ForReading, False)
    strFileData = oTS.readall()
    oTS.Close

    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = True
    oRE.Pattern = "(""B"".*)(\r\n)"

    If oRE.test(strFileData) Then
        strFileData = oRE.Replace(strFileData, "$1" & vbTab & "#This line is special$2")
    End If

    Set oTS = oFS.OpenTextFile("C:\Q_28615458.txt", ForWriting, False)
    oTS.write strFileData
    oTS.Close
	msgbox ("Done")
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40606381
You probably don't have permission to write to files in the root directory.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40606384
are you calling the routine in your VBScript?
0
 

Author Comment

by:acunaara
ID: 40606571
oh yes!! sorry

It's works perfect!! sorry again!!
0
 

Author Closing Comment

by:acunaara
ID: 40606590
Thanks!!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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