1.

Solve : Using FINDSTR to search for " in a file?

Answer»

I am running through debugging some code and I have come across several scripts that are in use where there is an issue with some of the VARIABLE declarations are missing a ". It is a proprietary scripting language based on BASIC, and I am using Windows XP's FINDSTR command to find all of the lines of code that contain ". Here is where I am so far:

quotefinder.bat:
Code: [SELECT]findstr /N /G:quote.txt \\remote\c-drive\testenvironment\%1 >temp.txt
quote.txt:
Code: [Select]"
This is returning temp.txt containing every line that contains any ". How would I go about elminating all of the lines that have an even number of " so that I can only see the lines that have an odd number of ", which would show me where the " errors are happening? Right now I am thinking about creating a file:

quoteclose.txt:
Code: [Select]".*"
If I understand CORRECTLY, this will give me all of the lines that contain PAIRS of quotes, and if so, I should be able to add this to quotefinder.bat:

Code: [Select]findstr /N /G:quoteclose.txt \\remote\c-drive\testenvironment\%1 >temp2.txt
And then from there work out my best OPTION to compare the two files and remove all lines duplicated from the two. I don't expect to have to use this batch file often, however, it will probably be run on more script files than I can even count at the moment as a debugging tool if I can get this working.
Try as I might, I couldn't figure out where batch code would store the match count. Decided to give you a VBScript solution instead:

Code: [Select]Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set objRE = CreateObject("VBScript.RegExp")
objRE.Global     = True
objRE.IgnoreCase = True
objRE.Pattern    = "\" & Chr(34)

Set f = fso.OpenTextFile("\\remote\c-drive\testenvironment\filename.ext", ForReading)
Set k = fso.OpenTextFile("c:\temp2.txt", ForWriting, True)

Do While f.AtEndOfStream = False
str = f.ReadLine
Set colMatches = objRE.Execute(str)
If colMatches.Count Mod 2 = 1 Then k.WriteLine str
Loop
f.Close
k.Close

Save the file with a vbs extension and run from a command prompt as cscript scriptname.vbs. You may have to adjust the paths and file names.

Good luck. OK, it looks like that VBS gives me most of what I needed. Now what I need is what to add to this script so the output shows me what line numbers on the input file I need to look at. I haven't used much VBS, and the system I am maintaining is rather archaic, so most tools that are at my disposal are homebrew. This being one of those instances where a syntax checker would be nice...  I tried to make the output look like the findstr output.

Code: [Select]Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set objRE = CreateObject("VBScript.RegExp")
objRE.Global     = True
objRE.IgnoreCase = True
objRE.Pattern    = "\" & Chr(34)

Set f = fso.OpenTextFile("\\remote\c-drive\testenvironment\filename.ext", ForReading)
Set k = fso.OpenTextFile("c:\temp2.txt", ForWriting, True)

lines = 0
Do While f.AtEndOfStream = False
str = f.ReadLine
lines = lines + 1
Set colMatches = objRE.Execute(str)
If colMatches.Count Mod 2 = 1 Then k.WriteLine lines & ":" & str
Loop
f.Close
k.Close

Good luck.



Discussion

No Comment Found