Solved

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

Posted on 2015-02-11
9
47 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
Comment Utility
please post a text file with the source data to be changed
0
 

Author Comment

by:acunaara
Comment Utility
this is the file
file.txt
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
Comment Utility
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
 

Author Comment

by:acunaara
Comment Utility
mmm It does not work
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:acunaara
Comment Utility
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
Comment Utility
You probably don't have permission to write to files in the root directory.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
are you calling the routine in your VBScript?
0
 

Author Comment

by:acunaara
Comment Utility
oh yes!! sorry

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

Author Closing Comment

by:acunaara
Comment Utility
Thanks!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

15 Experts available now in Live!

Get 1:1 Help Now