?
Solved

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

Posted on 2015-02-11
9
Medium Priority
?
58 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 46

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 46

Accepted Solution

by:
aikimark earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 46

Expert Comment

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

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

Independent Software Vendors: 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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

770 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