gpt4 book ai didi

windows - %variable% 和 !variable! 之间的区别在批处理文件中

转载 作者:可可西里 更新时间:2023-11-01 12:11:20 24 4
gpt4 key购买 nike

我正在编写一个批处理文件,我需要在其中输出一个包含“!”的字符串到另一个文件。但是当我将该字符串回显到另一个文件时,它会删除“!”从输出。

例如: 输入:

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

new_file.txt 中的输出是:

Hi this is output

此外,如果输入是

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

new_file.txt 中的输出:

Hello

因此,它跳过了! (感叹号)从输出到 new_file。如果我使用 %LINE%,那么它只会在输出文件中显示“回声打开”。

请提出解决此问题的方法。

最佳答案

如果开启了延迟展开,想输出感叹号,需要转义。

转义感叹号不需要一个或两个插入符号,具体取决于位置。

@echo off
REM No escaping required, if delayed expansion is disabled
set test1=Test1!

setlocal EnableDelayedExpansion
REM One caret required
REM Delayed expansion uses carets independent of quotes to escape the exclamation mark
set "test2=Test2^!"

REM Two carets required
REM The first caret escapes the second caret in phase2 of the parser
REM Later in the delayed expansion phase, the remaining caret escapes the exclamation mark
set test3=Test3^^!


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

block 中 !var!%var% 之间的区别在 DOS batch: Why are my set commands resulting in nothing getting stored? 中有解释。

可以在 How does the Windows Command Interpreter (CMD.EXE) parse scripts? 找到批解析器的解释。

关于windows - %variable% 和 !variable! 之间的区别在批处理文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14354502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com