Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple regex

1 view
Skip to first unread message

Al

unread,
Aug 24, 2010, 5:05:35 PM8/24/10
to
Hi All,

I'm having an issue doing a regex find and replace using vbscript.
Here's my script:

Dim pattern(3)
Dim repl(3)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\file1.txt", 1)

strText = objFile.ReadAll
objFile.Close

pattern(1) = "^"
pattern(2) = "$"
pattern(3) = "\|"

repl(1) = """"
repl(2) = """"
repl(3) = ""","""

For i = 1 to 3

Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = "" & pattern(i) & ""
regEx.MultiLine = True

strNewText = regEx.Replace(strText, repl(i))

Next

Set objFile = objFSO.OpenTextFile("c:\file1.txt", 2)
objFile.WriteLine strNewText
objFile.Close


Basically, my script reads a file into a variable, loops through it 3
times replacing the start of each line with a quotation mark, replace
the end of each line with a quotation mark, and replace the pipe
character | with a quotation mark, comma, and another quotation Mark.

File1.txt looks like this:

122101 |20100826|SAMLP
122102 |20100827|SAMLP
122103 |20100823|SAMLP


My script generates this:

122101 |20100826|SAMLP "
"
122102 |20100827|SAMLP "
"
122103 |20100823|SAMLP "
"


What I want is:

"122101 ","20100826","SAMLP "
"122102 ","20100827","SAMLP "
"122103 ","20100823","SAMLP "

What am I doing wrong?

Thanks.

Al

unread,
Aug 24, 2010, 5:41:48 PM8/24/10
to
I removed the

regEx.MultiLine = True

and now I get

122101 ","20100826","SAMLP
122102 ","20100827","SAMLP
122103 ","20100823","SAMLP

But I'm still missing the quotes at the beginning and end of each
line. Help appreciated.

Thanks.

Al

unread,
Aug 25, 2010, 12:19:46 PM8/25/10
to
I got it.

my last problem to solve is

Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.MoveFile "*posn.txt", "posn.txt"

this generates a Path not found error.

The script and the file 123posn.txt are located in a network folder

0 new messages