0

I am trying to write a Windows batch script which examines an XML file (the iml file) to search for a certain line, . I am having trouble with the spaces in the string. Is there a way to escape or compensate for the spaces?

for /f "tokens=*" %%a in (abcd.iml) do (
    echo %%a
    if %%a==^<orderEntry type="inheritedJdk" ^> (echo 'FOUND')
 )
3
  • 5
    You should always use quotes with string comparisons. if "%%a"=="<orderEntry type="inheritedJdk" >" Commented Apr 23, 2020 at 20:25
  • 3
    …and (echo 'FOUND'^). Commented Apr 23, 2020 at 20:28
  • 1
    possible duplicate and possible duplicate Commented Apr 23, 2020 at 20:45

2 Answers 2

1

Put the %%a in quotes so it is like "%%a." Instead of using the "==" operator to compare %%a and "^" it would be better to use the EQU comparator so the final code would look like:

for /f "tokens=*" %%a in (abcd.iml) do (
    echo %%a
    if "%%a" EQU "<orderEntry type="inheritedJdk" >" (echo 'FOUND')
 )

For more information on EQU and other operators see https://ss64.com/nt/if.html or https://ss64.com/nt/equ.html

Sign up to request clarification or add additional context in comments.

4 Comments

Neko Musume, I think that you should have read Sqashman's opening comment, and my comment too, because currently, (despite the upvote you've already received) your answer doesn't work as intended.
Oh do you mean the breakout key on the echo? Sorry I never really understood how to use it but I can edit the answer and add it.
Please read the comments! and obviously if you get the chance test it yourself. It wouldn't take much to test it against any file, renamed accordingly with and without inserting the required string <orderEntry type="inheritedJdk" > on a line.
It already works for me, I've tested it so I don't know what errors you are talking about
1

no need for a for loop. A simple findstr is much faster:

 findstr /c:"<orderEntry type=\"inheritedJdk\" >" "abcd.iml" >nul && echo found || echo nope

Note: the quotes within the string have to be escaped (escape character for findstr is a backslash)

2 Comments

Wouldn't "findstr" be better for searching for text in files?
@NekoMusume findstr is newer and has more options, but find is perfectly fine for some tasks, so newer is not always better. But you are right in this case. The quotes in the string have to be escaped. Find can't do that (my previous find solution ran fine just "by accident" (was satisfied by just <orderEntry type= and ignored the rest)). Changed to findstr.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.