Unexpected results with VBS

Microsoft Windows xp professional w/serv...
July 8, 2010 at 09:17:15
Specs: Windows XP
The following 2 files accomplish:
- inserting lines of text below a found phrase in
a text file
- changing all instances of
"Log.someword();" to
"Log.somewordFormat();"
- keeps a log file noting all original text and the new text in each file changed

It was working before...and I'm not sure what I
did, but it now replaces every "Log" line in a
file with the same replacement text, even
though the indention and someword are
variable.

For example, say the file being searched originally has the text:
Log.Info("test");
indentedLog.Debug("test2");

The log outputs the correct changes:
Log.InfoFormat("test");
indentedLog.DebugFormat("test2")

However, the file is actually changed to:
Log.InfoFormat("test");
Log.InfoFormat("test2")

Which doesn't make sense because the replacement string is stored in a single variable that is used both in the log file and actual replacement. I put the area in the code that does this in bold below.

I'm sure I just have some sort of logic error
going here, but I've been staring at this file too
long to notice. Any ideas?

Batch File to run the script:
@echo off > newfile & setlocal
EnableDelayedExpansion

findstr /i /r ".*" < changelog.txt > changelog.txt

cscript logscript.vbs C:\tara\test

for /D /r %%G in ("test\*") do (
echo [%%G]
cscript logscript.vbs %%G
)

for /f "tokens=1*" %%i in (C:\tara\changelog.txt) do (
if /i "%%i" equ "file:" (
set /a count+=1
)
)

>> newfile echo filecount: !count!
type C:\tara\changelog.txt >> newfile
move /y newfile C:\tara\changelog.txt


VBS file
Set outFSO =
CreateObject("Scripting.FileSystemObject")
Set outFile =
outFSO.OpenTextFile("C:\tara\changelog.txt",
8,True)

outPath =
outFSO.GetAbsolutePathName("C:\tara\changelog.txt")
outName = outFSO.GetFileName(outPath)

Set fso = CreateObject("Scripting.FileSystemObject")
Set regEx = New RegExp
Set fld = fso.GetFolder(WScript.Arguments(0))

For Each File in fld.Files
If (StrComp(File.Type,"Visual C# Source file",1) = 0) Then

regEx.Pattern = "<autogenerated>"
regEx.Global = True
If Not(regEx.Test(fso.OpenTextFile(File).ReadAll
)) Then

Change()

End If
End If
Next


Sub Change()
mylog = ""

regEx.Pattern = "(.)*public(.)*class(.)*\n?(.)*{"
Set Matches = regEx.Execute(fso.OpenTextFile(File).ReadAll)

If Matches.Count > 0 Then
mylog = mylog & vbTab & "***** INSERT *****" & vbCRLF
mylog = mylog & vbTab & "old: " & vbCRLF & vbTab & Matches(0).Value & vbCRLF
temp = split(Matches(0).Value, "public")
indent = temp(0)
s = Matches(0).Value
s = s & vbCRLF
s = s & indent & vbTab & "test1" & vbCRLF
s = s & indent & vbTab & "test2" & vbCRLF
s = s & indent & vbTab & "test3" & vbCRLF
s = s & indent & vbTab & "test4" & vbCRLF
mylog = mylog & vbTab & "new: " & vbCRLF & vbTab & s & vbCRLF
txtFile = regEx.Replace(fso.OpenTextFile(File).ReadAll, s)
fso.OpenTextFile(File, 2, True).Write txtFile
End If

regEx.Pattern = "(.)*Log\.[a-zA-Z]{4,5}\("
Set Matches =
regEx.Execute(fso.OpenTextFile(File).ReadAll)

If Matches.Count > 0 Then
mylog = mylog & vbTab & "***** REPLACE *****" & vbCRLF

For Each Match in Matches
temp1 = split(Match.Value, "Log")
indent = temp1(0)

mylog = mylog & vbTab & "old: " & Match.Value & vbCRLF

temp = split(Match.Value,".")
middle = split(temp(1), "(")
t = indent & "Log." & middle(0) & "Format("

mylog = mylog & vbTab & "new: " & t & vbCRLF

txtFile = regEx.Replace(fso.OpenTextFile(File).ReadAll, t)
fso.OpenTextFile(File, 2, True).Write txtFile
Next

End If

If (StrComp(mylog,"") <> 0) Then
filecount = filecount + 1
outFile.WriteLine("file: " & File.Name & vbCRLF & mylog)
End If

End Sub


See More: Unexpected results with VBS

Report •


#1
July 12, 2010 at 07:01:54
Well I found out that when I use regex.Replace(), it replaces every instance of that regex. However, depending on the middle part of the match, the string that replaces it is different. That's why the code currently cuts out that middle variable part (middle) and keeps it in the new replacement string (t). Also, the indention changes from match to match.

Any ideas on how this might be done?


Report •
Related Solutions


Ask Question