Assume this batch file
call :SomeFunction "a string with an > arrow"
goto:eof
:SomeFunction
echo %~1
goto:eof
The output of this is
call :SomeFunction "a string with an > arrow" echo a string with an 1>arrow goto:eof goto:eof
and a file named arrow
is created which contains a string with an
. Note the 1>
.
How can I prevent the command processor from interpreting the >
as a redirection symbol in this situation? (Hint: I've tried ^>
and that's not it.)
EDIT: The other operators (|
and &
) are of course also affected.
You can use
FOR /F
command for that, instead of:Use this:
Edit: note that now you need to deal with strange escaping behaviors when you pass an argument delimited with double quotes. For example
"a^2"
will be escaped as"a^^2"
, no matters if you try with"a^^2"
or"a\^2"
. What you may do (from your own comment) is to use a temporary variable and do escaping (then removing double quotes):If you do not want to care about escaping you may also try:
Note double quotes to enclose
set
argument and many special characters in the string. In this casetmp
environment variable will literally be:"many \ ^ > ' characters and quotes"
, you can simply use it as:Note
%1
instead of"%~1"
. Let's now make it more complicated. If you need double quotes " inside your string then some characters won't be escaped (& and | for example). You may simply remove quotes:Used:
Or
Don't forget you can use backtips to delimit
FOR
string if you specifyusebackq
option. You may use a temporary file or...better...a PowerShell script...Call
does funky things with^
but this works.Like foxidrive mentioned, CALL will modify carets, so it's not possible to build a bullet proof way to transfer parameters by value via CALL.
But you can use parameters by reference instead.
I use here delayed expansion, as this is the best way to handle any variable content in a safe way