Solved

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

Posted on 2015-02-11
9
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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