Inside quoted strings ^
doesn't work as the escape character1. Therefore you don't need to escape twice with \^
, only \
is enough (in this case). When you run your command then 2 arguments will be passed to FINDSTR
: id type=\^eLocator"
and value="e app46707.txt
That's why you see the
FINDSTR: Cannot open value="e app46707.txt
error, as there's no file named value="e app46707.txt
. Besides FINDSTR
searches for multiple words by default. FINDSTR "abc xyz"
will find any lines that contain "abc" or "xyz". To find a literal string with spaces you must use the /C:
option. After fixing all of the above points then the command will work as expected
D:\>FINDSTR /c:"id type=\"eLocator\" value=\"e" app46707.txt
<id type="eLocator" value="e46707"/>
But that way it's highly fragile, because it'll break when more spaces or tabs are inserted in the xml tag, or "
is changed to '
, or the tag is broken into multiple lines. Moreover escaping in cmd is a mess that's difficult to fix. PowerShell has far more regular quoting rules, and it supports everything that's available in .NET framework, so it already has native capability to parse xml files. Just use Select-Xml
which will return the node based on an XPath query
PS D:\> Select-Xml -Path .\app46707.txt -XPath '//id[@type="eLocator"]'
Node Path Pattern
---- ---- -------
id D:\app46707.txt //id[@type="eLocator"]
1From How does the Windows Command Interpreter (CMD.EXE) parse scripts?
If it is a quote ("
) toggle the quote flag, if the quote flag is active, the following special characters are no longer special: ^ & | < > ( )
.