How to pass redirect symbols (< and >) to a Windows batch file function?

batch-file, cmd, windows

Solution

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...

Problem

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.

Original source