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</filena me>
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
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>
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</filena
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
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"
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%]
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%]
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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%]