How to pass redirect symbols (< and>) to a Windows

2019-03-27 18:17发布

问题:

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.

回答1:

You can use FOR /F command for that, instead of:

echo %~1

Use this:

FOR /F "delims=" %%i IN ("%~1") DO echo %%i

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):

set TEMP=%TEMP:>=^>%

If you do not want to care about escaping you may also try:

set "tmp="many \ ^ > ' characters and quotes""

Note double quotes to enclose set argument and many special characters in the string. In this case tmp environment variable will literally be: "many \ ^ > ' characters and quotes", you can simply use it as:

FOR /F "delims=" %%1 IN (%tmp%) DO echo %%1

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:

set "tmp=many \ ^ > ' | & characters and quotes"

Used:

FOR /F "delims=" %%1 IN ("%tmp%") DO echo %%1

Or

FOR /F "delims== tokens=2" %%1 IN ('set tmp') DO echo %%1

Don't forget you can use backtips to delimit FOR string if you specify usebackq option. You may use a temporary file or...better...a PowerShell script...



回答2:

Call does funky things with ^ but this works.

@echo off
set "var=a string with an ^> arrow"
call :SomeFunction 
pause
goto:eof

:SomeFunction
  echo %var%
goto:eof


回答3:

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.

@echo off
set "var=a string with a caret^ and an > arrow"

call :SomeFunc var
exit /b

:SomeFunc
setlocal EnableDelayedExpansion
set "param1=!%1!"
echo !param1!
exit /b

I use here delayed expansion, as this is the best way to handle any variable content in a safe way