[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 724
  • Last Modified:

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
0
mfuerlinger
Asked:
mfuerlinger
  • 3
  • 2
1 Solution
 
oBdACommented:
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%]
0
 
mfuerlingerAuthor Commented:
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"
0
 
oBdACommented:
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%]
0
 
mfuerlingerAuthor Commented:
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
0
 
oBdACommented:
With this:

@echo off
setlocal enabledelayedexpansion
set XMLFile=test.xml
set FileName=
for /f "tokens=1,3 delims=[]^<^>" %%a in ('type "%XMLFile%" ^| find /n /i "file_name"') do (
  if "!FileName!"=="" (
    set FileLine=%%a
    set FileName=%%b
  )
)
echo Filename: [%FileName%] in line [%FileLine%]
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%]
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now