变量未按预期运行 [英] Variables are not behaving as expected
问题描述
我一直在努力使这个批处理文件的语法正确,但我无法弄清楚为什么有些东西不起作用.
I've been wrestling trying to get the syntax right on this batch file and I cannot figure out why some things aren't working.
- 变量
i
没有在我每次执行时递增. strc
上的串联似乎没有串联.
- The variable
i
is not getting incremented each time I do it. - Concatenation on
strc
doesn't seem to concatenate.
这是我的代码:
set i=0
set "strc=concat:"
for %%f in (*.mp4) do (
set /a i+=1
set "str=intermediate%i%.ts"
set strc="%strc% %str%|"
ffmpeg -i "%%f" -c copy -bsf:v h264_mp4toannexb -f mpegts "%str%"
)
set strc="%strc:-1%"
ffmpeg -i "%strc%" -c copy -bsf:a aac_adtstoasc Output.mp4
推荐答案
你不是第一个掉入著名的延迟扩张陷阱"的人(而且你不会是最后一个).
You are not the first, who fell into the famous "delayed expansion trap" (and you won't be the last).
如果要使用在同一块中更改的变量,则需要延迟扩展(块是括号内的一系列命令(
and )
).
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within parentheses (
and )
).
延迟变量使用 !var!
而不是 %var%
引用.
Delayed variables are referenced with !var!
instead of %var%
.
原因是方式,cmd
解析代码.一次完整的行或块被解析,在解析时用它们的值替换普通变量.延迟变量在运行时计算.
Reason is the way, cmd
parses the code. A complete line or block is parsed at once, replacing normal variables with their value at parse time. Delayed variables are evaluated at runtime.
两个简单的批处理文件来演示:
Two simple batch files to demonstrate:
setlocal EnableDelayedExpansion
set "var=hello"
if 1==1 (
set "var=world"
echo %var% !var!
)
setlocal EnableDelayedExpansion
for /L %%i in (1,1,5) do (
echo %random% !random!
)
注意:一行也被视为一个块:
Note: A line is also treated as a block:
set "var=old"
set "var=new" & echo %var%
延迟扩展:
setlocal EnableDelayedExpansion
set "var=old"
set "var=new" & echo !var!
延迟扩展在命令提示符下默认关闭.如果你真的需要它,你可以这样做:
Delayed expansion is per default turned off at the command prompt. If you really need it, you can do:
cmd /V:ON /C "set "var=hello" & echo !var!"
还有一种方法可以在不延迟扩展的情况下执行相同操作(但是 call
需要花费一些时间,因此速度较慢,但是如果由于某种原因您不能/不想使用延迟扩展,这是一个替代方案):
Also there is a way to do the same without delayed expansion (but call
costs some time, so it's slower, but if for some reason you can't / don't want to use delayed expansion, it's an alternative):
setlocal DISabledelayedexpansion
for /L %%i in (1 1 5) do (
call echo %random% %%random%%
)
这两种方法也可用于显示类似数组的变量:
Both methods can also be used to display array-like variables:
(这通常被问及包含另一个变量的变量"或嵌套变量")
(This is often asked like "variable which contains another variable" or "nested variables")
这是在不同情况下使用此类类似数组的变量的集合:
Here is a collection for using such array-like variables in different situations:
延迟扩展:
setlocal ENableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
echo plain delayed: !var[%num%]!
for /L %%i in (4 1 4) do (
echo for delayed: !var[%%i]!
set a=%%i
call echo for delayed with variable: %%var[!a!]%%
)
无延迟扩展:
setlocal DISableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
call echo plain called: %%var[%num%]%%
for /L %%i in (4 1 4) do (
call echo FOR called: %%var[%%i]%%
set a=%%i
call echo FOR called with variable: %%var[%a%]%%
)
注意:setlocal
在批处理文件之外没有任何作用,因此 delayedexpansion
仅适用:
Note: setlocal
has no effect outside of batchfiles, so delayedexpansion
works only:
- 在批处理文件中
- 当 cmd 在启用延迟扩展的情况下启动时 (
cmd/V:ON
)(默认情况下,cmd 在延迟扩展禁用启用的情况下运行)
- In batch files
- When the cmd was started with delayed expansion enabled (
cmd /V:ON
) (by default, the cmd runs with delayed expansion disabled)
这篇关于变量未按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!