Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
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 same1. 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 variable2. 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 etc3. How to check if a string is a Numeric or not? I wanted only the numeric to validate something.
Thanks in advance
Regards
Kamath

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 Next Loop
outputC:\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="1for 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.

Hi,
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
Regards
Aravind

Hi
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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |