Find a string and collect the next 3 digits

October 23, 2009 at 02:50:54
Specs: Windows XP

I am a beginner in Batch scripting but managed to write the piece of code to automate some stuff. It’s a little CRUDE method that I am following as of now. I am struck with some 3 simply issues. Once fixed I will try to make it more robust and very crisp. I am not able to figure out even on googling.
Hence requesting your help here to fix the same

1. I have saved one XML as a txt file and processing the same. Below are 2 sample piece of code.
<message field1="10" field2="Title" Field3="My description for this" Field4="0" Filed5="60" Filed6="20” />
<message field1="11" field2="hello" Field3="damn difficult" Field4="1" Filed5="0" Filed6="0” />

Let us assume only 1st line(highlighted in Bold) is there in my TXT file. I need to collect
• Field4=”0” into a variable called Var
• Field5=”60” into a variable called Var1
How can I do that? I am not very sure how to process a line and pick up a string into a variable

2. Say a variable v1=”123456 ”
I am able to check the 8th bit as blank with the code below %v1:~7,1%
IF %var:~7,1%==" " echo blank
But I wanted to check if the 8th bit is equal to “. It looks like the Double quote is a special character and hence the screen just finishes the execution. It doesn’t do as intended. I tried something like this
IF %var:~8,1%==" echo blank ------ Doesn’t work
IF “%var:~8,1%”==" echo blank ------- Doesn’t work
IF %var:~8,1%==\" echo blank ------- to remove some special meaning etc

3. How to check if a string is a Numeric or not? I wanted only the numeric to validate something.

Thanks in advance


See More: Find a string and collect the next 3 digits

Report •

October 23, 2009 at 06:54:12
using batch to parse file, especially files with XML structure is just asking for trouble. The ideal method to parse HTML/XML files is to use a tool/programming language with libraries that makes manipulating HTML/XML files easier for the human being. Such languages may be Perl, Python etc. If that option is not available, at least use a tool that is meant for text/string manipulation. eg Perl, Python, awk, even vbscript makes string manipulation easier.

since you are a beginner, i suggest you take the time to learn something else. But in the meantime, here's a vbscript
that shows you how to do it the easier way..

Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
q = """" 'double quote
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine
	s = Split(strLine,q&" ")
	For i=LBound(s) To UBound(s)		
		If InStr(s(i),"Field4")> 0 Then
			WScript.Echo "I found field 4 ", s(i)	
		ElseIf InStr(s(i),"Field3") > 0 Then
			WScript.Echo "I found field 3", s(i)
		End If 	

C:\test>cscript /nologo test.vbs
I found field 3 Field3="My description for this
I found field 4  Field4="0
I found field 3 Field3="damn difficult
I found field 4  Field4="1

for you other questions,
1) check for blanks at 8th character, you can use Mid() function together with if/else
2) to check numeric, there is the IsNumeric() function.

GNU win32 packages | Gawk

Report •

October 24, 2009 at 06:52:11
Thanks a lot for the quick reply :)

I had done some basic Batch scripting before. This is the 1st time I am doing something on Parsing a file.

The client requirement is BATCH only for some reason. So I am left with no option other than BATCH. I also understand that BATCH scripting is very tedious and is kind of obsolete. Hence I request your help for my questions asked above.

Thanks in advance


Report •

October 27, 2009 at 14:21:52
Do you only want to assign Fields 4 and 5 to Variables or all the fields to variables, and then test which ones are numeric.

Report •

Related Solutions

Ask Question