如何通过重定向符号(LT;和&gt;),以Windows批处理文件功能? [英] How to pass redirect symbols (< and >) to a Windows batch file function?
问题描述
假设这个批处理文件
call :SomeFunction "a string with an > arrow"
goto:eof
:SomeFunction
echo %~1
goto:eof
的这个输出是
call :SomeFunction "a string with an > arrow"
echo a string with an 1>arrow
goto:eof
goto:eof
和一个名为箭头
创建包含一个字符串的
。注意 1方式&gt;
and a file named arrow
is created which contains a string with an
. Note the 1>
.
我怎样才能prevent从国际preting命令处理器的&GT;
在这种情况下,一个重定向符号? (提示:我试过 ^&GT;
这不是吧)
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.)
编辑:在其他运营商( |
和&安培;
),当然也影响了
The other operators (|
and &
) are of course also affected.
推荐答案
您可以使用 FOR / F
命令,代替:
You can use FOR /F
command for that, instead of:
echo %~1
使用这样的:
FOR /F "delims=" %%i IN ("%~1") DO echo %%i
编辑:注意,现在你需要传递时,用双引号分隔的参数来处理怪逃跑的行为。例如一^ 2
将逃跑的如一^^ 2
如果你试图用没有问题一^^ 2
或一\\ ^ 2
。你可以这样做(从您自己的评论)是使用一个临时变量,做逃逸(然后去除双引号):
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""
请注意双引号括设置
参数和许多的特殊的字符串中的字符。在这种情况下 TMP
环境变量确实将:多\\ ^&GT;'字符和引号
,你可以只是作为:
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
请注意%1
而不是的%〜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"
用于:
FOR /F "delims=" %%1 IN ("%tmp%") DO echo %%1
或者
FOR /F "delims== tokens=2" %%1 IN ('set tmp') DO echo %%1
不要忘了,你可以使用backtips划的
字符串,如果您指定有usebackq
选项。您可以使用一个临时文件或... ...好PowerShell脚本...
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...
这篇关于如何通过重定向符号(LT;和&gt;),以Windows批处理文件功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!