tags:

views:

279

answers:

2

Question about Batch/Windows/CMD:

I would like that my batch file can search for a line (which I already achieved, but what comes next not), it looks like this:

<name>MyName</name>

It needs to find the text in between <name> and </name>. After that it needs to set that as a variable (%name%).

Does anyone have any idea?

EDIT: if someone wants to give an answer, please list the code. Perl is OK, but this should be open-source and not everyone has Perl.

A: 

Learn Perl, it's made for exactly that kind of thing.

mcandre
Now you have 2 problems. http://blogs.msdn.com/oldnewthing/archive/2006/03/22/558007.aspx
RossFabricant
No, really, Perl is great for manipulating text files. Let me add to your joke: simply using a computer gives you two problems. Opening a browser and entering a question on SO gives you two problems. If you don't want to learn how to use good tools, good luck accomplishing anything.
mcandre
the link you give is just 'a little' outdated... i know what your going to say 'it are the basics'... But if you want to give me a tutorial, please give me a more updated one, ok. But thanks anyways.
YourComputerHelpZ
Find them online or buy an O'Reilly Perl book.
mcandre
+3  A: 

It can be done this way (assuming your input is in file "test1.html"):

findstr "<name>" test1.html > temp1.lis
FOR /F "tokens=2 delims=>" %%i in (temp1.lis) do @echo %%i > temp2.lis
FOR /F "tokens=1 delims=<" %%i in (temp2.lis) do @echo %%i > temp3.lis

The first line is a guard that only HTML/XML tag "name" will match in the two FOR lines (you may already have done this). The result is saved in a temporary file, "temp1.lis".

The second line capture what is to the right of the first ">" - in effect what is after "<name>". At this stage "MyName</name" is left in temporary file "temp2.lis" (as the closing tag also contains ">"). Note the double "%"s (%%i) as this is in a BAT file (if you want to test directly from the command line then it should only be one "%").

The third line capture what is to the left of the first "<" - this is the desired result: "MyName" (is left of "<" in "MyName</name"). The result is in variable %%i and you can call a function with %%i as a parameter and access the result in that function (in the FOR line above the function was the built-in "echo" and the result thus ended up in temporary file "temp3.lis" by the redirection of standard output)


Note that the above only works if

<name>MyName</name>

is the first HTML/XML tag in a line.

If that is not the case or you want a more robust solution you can instead call a function in the first FOR line (that receives %%i as the first parameter). That function can then replace "<name>" with a single character that you are sure is not in the input, e.g.:

set RLINE=%MYLINE:<name>=£%

Explanation: if the input line is in variable %MYLINE% then "<name>" will be replaced with "£" and the result will be assigned to variable %RLINE%.

The reason for the replace is that the delimiters for the FOR loop are single character only.

You can then use "£" as a delimiter in the FOR loop (to extract what is to the right of "<name>" - as before):

echo %RLINE%>temp5.lis
FOR /F "tokens=2 delims=£" %%i in (temp5.lis) do @echo %%i > temp6.lis

You have to repeat this technique for "</name>" (but only if <name>MyName</name> is not the first HTML/XML tag in a line).

So as you see it is possible, but is quite painful.

Peter Mortensen
you say that above only works if is the first line... but you use findstr so it searches in the hold doc right?
YourComputerHelpZ
No, not the first line. The first tag in a line. If a line contained "<someOtherTag>Unrelated content</someOtherTag> some more words <name>MyName</name>" then the relatively simple first solution would not work.
Peter Mortensen
Can you list some sample input in your question?
Peter Mortensen
o no thats totally fine. Every tag is done on a seperate line... So it's ok.
YourComputerHelpZ
can you give me a total code at the first one (so </name> also included)
YourComputerHelpZ
What do you mean by "at the first one"? The line with "findstr"?
Peter Mortensen
the hole script, i mean can you make it total, becaue you said "You have to repeat this technique for "</name>"."
YourComputerHelpZ
But you don't need it as there is only one HTML/XML tag per line for your input data. You can just use the 3 lines in the beginning of the answer (I will edit my answer to make this more clear).
Peter Mortensen
forget about it, its working like a charm. i see that was only meant for whats after the first code... Thanks!!!
YourComputerHelpZ
No problem, I am glad to help.
Peter Mortensen