%变量%和!变量之间的区别!在批处理文件 [英] Difference between %variable% and !variable! in batch file

查看:305
本文介绍了%变量%和!变量之间的区别!在批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个批处理文件,我需要输出包含字符串'!到另一个文件。但是,当我赞同这个字符串到另一个文件中,它将删除!从输出。

I am writing a batch file where I need to output a string containing '!' to another file. But when I echo that string to another file, it removes "!" from the output.

例如:
 输入:

Eg: Input:

set LINE=Hi this is! output
echo !LINE!>>new_file.txt

在new_file.txt输出是:

Output in new_file.txt is:

Hi this is output

此外,如果输入是

Also, if input is

set LINE=Hello!! this is output!!
echo !LINE!>>new_file.txt

在new_file.txt输出:

Output in new_file.txt:

Hello

因此​​,它跳过了!从输出到NEW_FILE(感叹号)。
如果我使用%的线路%,那么它只是显示回声的输出文件。

Hence, it skips the ! (Exclamation mark) from the output to the new_file. If I use %LINE%, then it simply displays "echo is on" to the output file.

请提供一个方法来克服这个问题。

Please suggest a way to overcome this problem.

推荐答案

如果您已启用了推迟扩张和要输出一个感叹号,就需要逃避它。

If you have delayed expansion enabled and want to output an exclamation mark, you need to escape it.

感叹号避灾需要无,一个或两个插入,这取决于位置。

Escaping of exclamation marks needs none, one or two carets, depending on the placement.

@echo off
set test1=Test1!
setlocal EnableDelayedExpansion
set test2=Test2^^!
set "test3=Test3^!"

echo !test1!
echo !test2!
echo !test3!

!无功!%VAR%块之间的差异是在的 DOS批处理:为什么是导致没有得到存储在我的命令集

The difference between !var! and %var% in blocks is explained at DOS batch: Why are my set commands resulting in nothing getting stored?

批次解析器的解释可以在如何Windows命令国米preTER(CMD.EXE)解析脚本中找到?

An explanation of the batch parser can be found at How does the Windows Command Interpreter (CMD.EXE) parse scripts?

这篇关于%变量%和!变量之间的区别!在批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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