简体   繁体   English

批量使用剪辑时转义字符

[英]Escape Characters When Using Clip in Batch

The command that works on the command line is for /f "tokens=1* delims= " %A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"') Do (echo %B)在命令行上运行的命令是for /f "tokens=1* delims= " %A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"') Do (echo %B)

In a batch that becomes for /f "tokens=1* delims= " %%A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"') Do (echo %%B) which works在变成for /f "tokens=1* delims= " %%A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"') Do (echo %%B)有效

But to copy to clip board I need to do more with the escape character in the 2^>NUL^|但是要复制到剪贴板,我需要对2^>NUL^|中的转义字符进行更多操作. . In batch the closest I've got it echo.for /f "tokens=1* delims= " %%A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^^^>NUL^^^|find "Address:"') Do (echo %%B)|clip .在批次中,我得到了最接近的echo.for /f "tokens=1* delims= " %%A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^^^>NUL^^^|find "Address:"') Do (echo %%B)|clip

The output of which is for /f "tokens=1* delims= " %A in ('nslookup myip.opendns.com. resolver1.opendns.com 2>NUL|find "Address:"') Do (echo %B) which fails as 2>nul|其输出是for /f "tokens=1* delims= " %A in ('nslookup myip.opendns.com. resolver1.opendns.com 2>NUL|find "Address:"') Do (echo %B)失败为2>nul| is not escaped properly.没有正确转义。

I tried adding a forth or even a fifth caret to 2>NUL|我尝试向2>NUL|添加第四个甚至第五个插入符号to try clip the output as 2^>nul^|尝试将输出剪辑为2^>nul^| but I can't figure it out.但我想不通。 Is it possible?可能吗?

Your command line is parsed several times, so you must use enough escaping in order to hide special characters from all of them:你的命令行被解析了好几次,所以你必须使用足够的转义来隐藏所有的特殊字符:

echo for /F "tokens=1* delims= " %%A in ('nslookup myip.opendns.com. resolver1.opendns.com  2^^^^^^^> nul ^^^^^^^| find "Address:"'^) Do (echo(%%B^) | clip

At first the whole command line is parsed, which results in the following line:首先解析整个命令行,结果如下:

 echo for /F "tokens=1* delims= " %%A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^^^> nul ^^^| find "Address:"') Do (echo(%%B) | clip

Next each side of the pipe ( | ) is parsed again, so the left side results in this line:接下来再次解析管道 ( | ) 的每一侧,因此左侧结果为这一行:

 echo for /F "tokens=1* delims= " %%A in ('nslookup myip.opendns.com. resolver1.opendns.com 2^> nul ^| find "Address:"') Do (echo(%%B)

As you can see I also escaped the closing parentheses, so the code may also even be used within a parenthesised block of code.如您所见,我还转义了右括号,因此代码甚至可以在带括号的代码块中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM