Page 1 of 1

URGENT: Parsing a line using Batch

Posted: 25 Oct 2009 01:23
by mynameiskamath
Hi,
For some reason the requirement is BATCH scripting from the client. I am a beginner in Batch scripting but managed to write the piece of code to automate some stuff. I am struck with some 3 simply issues. 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

Regards
Kamath

Posted: 26 Oct 2009 08:36
by avery_larry
A for statement will allow you to pull pieces out of a string, provided that you are able to consistently uniquely identify what you want. In this case:

<message field1="10" field2="Title" Field3="My description for this" Field4="0" Filed5="60" Filed6="20” />

I would presume we will always have the format of message and all the fields listed. In that case, we can separate the string into pieces that are split up (delimited) by whitespace and equal signs. Further, once split like that, we want the 9th piece (token) and the 11th piece in the line. We can use the custom "delims" and "tokens" with a standard for command like this:

Code: Select all

for /f "tokens=9-11 delims== " %%a in (yourfile.txt) do set var=%%a&& set var1=%%c

Which, of course, will be run against every line in the file. You could do this also:

Code: Select all

set mystring=<message field1="10" field2="Title" Field3="My description for this" Field4="0" Filed5="60" Filed6="20” />
for /f "usebackq tokens=9-11 delims== " %%a in ('%mystring%') do set var=%%a&&set var1=%%c


Note I didn't test those, but they should work (the 2nd one may have problems with the embedded double quotes).

For the double quote:

Code: Select all

IF ^%var:~8,1%==^" echo double quote


To test for numeric -- use findstr and regular expressions to find anything that isn't a number:

Code: Select all

set "testvar=1203974e"
echo.%testvar%|findstr /r "[^0-9]">nul 2>nul
if errorlevel 1 (
   echo The variable only contains numbers.
   ) else (
      echo The variable is not numeric only.
)

Posted: 27 Oct 2009 21:37
by arkamath7
Hi,
1st of all my sincere thanks to you for replying to my query. I think I have almost resolved this issue. I use a combination of few stufff.....Because of some time crunch I am not able to dump the code...

Once I complete I will post How is that i could do it. Then we can have some further discussion

Thanks once again.

Regards
Aravind

Posted: 28 Oct 2009 18:04
by ghostmachine4
the ideal solution for parsing XML stuff is to use a XML parser. You are bound to get into trouble by doing in pure batch code like that.