如何将大于和小于添加到批处理文件变量中 [英] How can I add greater than and less than into a batch file variable

查看:146
本文介绍了如何将大于和小于添加到批处理文件变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试

@echo off
set PTag=^<BR^>
echo %PTag%

我什么也没得到.

现在有趣的是,如果在最后一次回声之后有一个空行,我会得到:

Now what's interesting is that if there's an empty line after the last echo, I get:

The syntax of the command is incorrect.

如果我删除了@echo,那么它实际上会输出

If I remove the @echo off, then it actually outputs

echo <BR>

我想在变量内添加各种HTML标记,然后将这些变量连接起来以创建将在文件中输出的HTML.

I want to add various HTML tags inside variables and then concatenate these variables to create an HTML that I will output in a file.

推荐答案

set PTag=^<BR^>将值<BR>设置为PTag

当您运行echo %PTag%时,它将展开为echo <BR>,这是无效的重定向.您需要使用此

When you run echo %PTag% it expands to echo <BR> which is an invalid redirection. You need to escape the < and > inside PTag by using this

set PTag=^^^<BR^^^>

第一个^会自行转义,然后下一个<>

The first ^ escapes itself, then the next one escapes < or >

您也可以使用此

set "PTag=^<BR^>"


第二种原因:引号^失去其特殊含义


Reason for the second way: inside quotes ^ loses its special meaning

如果是引号("),请切换引号标志;如果引号标志处于活动状态,则以下特殊字符不再特殊:^ & | < > ( ).

If it is a quote (") toggle the quote flag, if the quote flag is active, the following special characters are no longer special: ^ & | < > ( ).

Windows命令解释器(CMD.EXE)如何解析脚本?

大多数特殊字符(^ & ( ) < > |以及标准定界符, ; = SPACE 一旦将它们放置在""之间,并且""本身不成为变量值的一部分, TAB )便失去其特殊含义.

most special characters (^ & ( ) < > | and also the standard delimiters , ; = SPACE TAB) lose their particular meaning as soon as ther are placed in between "", and the "" themselves do not become part of the variable value

批处理文件中的特殊字符


现在,变量内部将具有值^<BR^>,它将把echo %PTag%扩展为


Now the variable will have the value ^<BR^> inside it, and it'll expand echo %PTag% to

echo ^<BR^>

这是有效的命令

这篇关于如何将大于和小于添加到批处理文件变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆