Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript search and replace charactors

Posted on 2007-07-20
5
Medium Priority
?
1,075 Views
Last Modified: 2012-06-27
Hi,

I am looking for a VBScript to look inside a directory c:\testing\in and then for each file in there search and replace these charactors,

change ¬AMPERSAND¬      to &      
change ¬OPEN_TAG¬      to <
change ¬CLOSE_TAG¬      to >

I have this scripy which replaces spaces with tildas so hopefully its easy to amedn to do multiple search and replace s?

------------------
    Dim DirIn
    Dim DirOut
    Dim fso
    Dim oFolder
    Dim oFileIn
    Dim oFileOut
    Dim txtStream
    Dim txtOut
    Dim txtStreamOut
   
    DirIn = "c:\testing\in\"
    DirOut = "c:\testing\out\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.getFolder(DirIn)
   
    For Each oFileIn In oFolder.Files
       
        Set txtStream = fso.OpenTextFile(oFileIn)
       
            txtout = ""
        Do While Not (txtStream.atEndOfStream)
            txtOut = txtOut + Replace(txtStream.ReadLine, " ", "~")
        Loop
       
        Set txtStreamOut = fso.OpenTextFile(DirOut + oFileIn.Name, 2, True)
       
        txtStreamOut.write txtOut
       
        txtStreamOut.Close
        txtStream.Close
       

    Next

------------------
cheers

Rob
0
Comment
Question by:arundelr
  • 3
  • 2
5 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 19530564
Replace:

            txtOut = txtOut + Replace(txtStream.ReadLine, " ", "~")

with:

            txtOut = txtOut + Replace(Replace(Replace(Replace(txtStream.ReadLine, " ", "~"), "AMPERSAND", "&"), "OPEN_TAG", "<"), "CLOSE_TAG", ">")
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 19530584
arundelr,

Note that by default Replace() is case-sensitive.  So, OPEN_TAG <> Open_Tag.  Do you need it
to be case-INsensitive?

Regards,

Patrick
0
 

Author Comment

by:arundelr
ID: 19530682
Hi,

Worked a treat ;)

No I dont need it to be case INsensitive, but out of intrest how would you achive that?

Rob
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 19530711
arundelr,

Replace() has an optional argument to control case matching.

Replace(InputStr, "START", "<", , , vbTextCompare)

would make the comparison case-insensitive.

Regards,

Patrick
0
 

Author Comment

by:arundelr
ID: 19530818
Thank you
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

810 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