Solved

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

Posted on 2015-02-11
9
50 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
 

Author Comment

by:acunaara
ID: 40606256
mmm It does not work
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

20 Experts available now in Live!

Get 1:1 Help Now