gpt4 book ai didi

windows - 如何在 Windows 批处理脚本中保留 "delayed variable expansion"

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

这是我的脚本:

@echo off
setlocal

for /f %%i in ('echo aaa/') do set REPO=%%i
if "%REPO%"=="" (
echo No input
) else (
echo %REPO:~-1%
echo %REPO:~0,-1%
if %REPO:~-1%==/ set REPO=%REPO:~0,-1%
echo %REPO%
)

endlocal

请注意:

c:\dev\shunra\GlobalLibrary\Server>c:\Utils\hgbackup.cmd
/
aaa
aaa/

c:\dev\shunra\GlobalLibrary\Server>

这是怎么回事?

编辑

请注意,我正在为 REPO 分配一些计算结果为“aaa”的东西,因此我希望它打印“aaa”,而不是“aaa/”。这让我发疯。

EDIT2

显然,这是罪魁祸首(来自 set 命令的帮助):

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%

但我尝试使用 ! 符号,它仍然对我不起作用。我使用 get ! 打印在屏幕上还是错误的结果。

最佳答案

正如评论中所讨论的那样,在您编辑的问题中,您需要延迟扩展。

必须启用延迟扩展才能使用它。在批处理脚本中,您可以使用 setlocal enableDelayedExpansion

@echo off
setlocal enableDelayedExpansion

for /f %%i in ('echo aaa/') do set REPO=%%i
if "%REPO%"=="" (
echo No input
) else (
echo %REPO:~-1%
echo %REPO:~0,-1%
if %REPO:~-1%==/ set REPO=%REPO:~0,-1%
echo !REPO!
)

endlocal

编辑

如果 IN() 子句发生更改,导致 REPO 未定义,则上述操作将失败。例如:in (echo.)

失败是因为整个 IF/ELSE 结构必须有有效的语法,即使 ELSE 子句也不会被执行。

如果 REPO 未定义,则

if %REPO:~-1%==/ set REPO=%REPO:~0,-1%

扩展为

if ~-1REPO:~0,-1

这是无效的语法。

问题再次通过延迟扩展解决。

@echo off
setlocal enableDelayedExpansion

for /f %%i in ('echo.') do set REPO=%%i
if "%REPO%"=="" (
echo No input
) else (
echo %REPO:~-1%
echo %REPO:~0,-1%
if !REPO:~-1!==/ set REPO=%REPO:~0,-1%
echo !REPO!
)

endlocal

关于windows - 如何在 Windows 批处理脚本中保留 "delayed variable expansion",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13202895/

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