Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 756
Tomalak Avatar asked Jan 28 '14 12:01

Tomalak


People also ask

What does %% mean in batch script?

%%i is simply the loop variable. This is explained in the documentation for the for command, which you can get by typing for /? at the command prompt.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


2 Answers

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

like image 58
Adriano Repetti Avatar answered Oct 14 '22 07:10

Adriano Repetti


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
like image 34
foxidrive Avatar answered Oct 14 '22 06:10

foxidrive