使用感叹号的Windows批处理文件语法
在检查Axis2二进制发行版中axis2server.bat文件的详细信息时,我看到其中一行包含如下文本:使用感叹号的Windows批处理文件语法,windows,batch-file,Windows,Batch File,在检查Axis2二进制发行版中axis2server.bat文件的详细信息时,我看到其中一行包含如下文本: FOR %%c in ("%AXIS2_HOME%\lib\*.jar") DO set AXIS2_CLASS_PATH=!AXIS2_CLASS_PATH!;%%c 下面带两个感叹号的部分是什么意思 !AXIS2_CLASS_PATH! 名称中包含%的平均变量,不确定是什么!标记批处理文件中的平均值。当启用延迟扩展和更改或设置循环中的变量时,则!变数语法允许您在循环中使用变量 缺点
FOR %%c in ("%AXIS2_HOME%\lib\*.jar") DO set AXIS2_CLASS_PATH=!AXIS2_CLASS_PATH!;%%c
下面带两个感叹号的部分是什么意思
!AXIS2_CLASS_PATH!
名称中包含%的平均变量,不确定是什么!标记批处理文件中的平均值。当
启用延迟扩展
和更改
或设置循环中的变量时,则!变数代码>语法允许您在循环中使用变量
缺点是代码>成为延迟扩展的有害字符。正如foxidrive提到的,这与延迟扩展相关。通过在cmd提示符中运行帮助集
,可以找到更多信息,该提示符有以下说明:
Finally, support for delayed environment variable expansion has been
added. This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)
would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:
set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%
in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:
for %i in (*) do set LIST= %i
which just keeps setting LIST to the last file found.
Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)
set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%
由于变量扩展的工作方式,请参见@AlexK。谢谢,这很有帮助。如果你能补充一个答案,我想把这个问题标记为已回答。这回答了这个问题,但你能对此进行一点扩展吗?也许是一个简单的例子?