Link to home
Start Free TrialLog in
Avatar of mfuerlinger
mfuerlinger

asked on

Output of FIND cmd in a %VARIABLE% ?

Hi experts,

I'm not very familiar with MS-DOS batch cmds - hope someone of you can help.

How do I get the output of the FIND cmd in a %VARIABLE% ?

I'm trying to isolate some content of a long XML-file
The desired content is loctated in the <file_name>...</file_name> section of the XML-file

An example part of the XML-file:
<XML>
....
<file_name>here is what I want to grep</file_name>
....


Here is my use of FIND so far:
FIND /N /I "file_name" "test.xml"

The above FIND cmd offers the found linenumber an the content of the whole line.
Output on screen looks like:
---------- test.XML
[77]        <file_name>188.013</filename>

How can I get this output in a variable? To make further manipulation with it (e.g. to chomp it.)
I'm only interested in the "188.013" part.

I'm using W2K & cmd.exe
thanks for your help
mathias
Avatar of oBdA
oBdA

Try that (assuming you need it in a batch file):

@echo off
setlocal
set XMLFile=test.xml
for /f "tokens=1,3 delims=[]^<^>" %%a in ('type "%XMLFile%" ^| find /n /i "file_name"') do (
  set FileLine=%%a
  set FileName=%%b
)
echo Filename: [%FileName%] in line [%FileLine%]
Avatar of mfuerlinger

ASKER

thanks oBdA
changed your "for routine" a bit: to get "188.013" as %FileName%

for /f "tokens=1,3 delims=^<^>" %%a in ('type "%XMLFile%" ^| find /n /i "file_name"') do (
  set FileLine=%%a
  set FileName=%%b
)

works fine.
but how can I avoid different lenght of the %FileName% variable ?
e.g.
the format should always be xxx.xxx (3digits.3digits) even "0" should be "000"
and "13" should be "013"
Add that (the "1" at the beginning is to avoid comparing numbers with leading zeroes, which are interpreted as octal numbers):

for /f "tokens=1,2 delims=." %%a in ("%FileName%") do (
  set f1=%%a
  set f2=%%b
)
if 1%f1% LSS 110 set f1=0%f1%
if 1%f1% LSS 1100 set f1=0%f1%
if 1%f2% LSS 110 set f2=0%f2%
if 1%f2% LSS 1100 set f2=0%f2%
set FileName=%f1%.%f2%
echo Filename: [%FileName%]
thanks again,
promise my last sub-question.
everything works fine, except I have XML-files with two ore more <file_name> ... </file_name> sections.
How can I force the above find cmd to allways use only the first <file_name> entry and ignore other <file_name>entries in the XML-file
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial