我正在尝试编写一个看似简单的脚本,但我想不通。
基本上,用户会被问到问题 1:“他们想要将多少(在本例中)视频文件加在一起以创建 1 个大视频文件?”
然后用户会被问到问题 2:“您要一起添加的文件的名称是什么?”现在这是我遇到的问题......
我如何创建一个 for 循环来询问问题 2 第一个问题中给出的次数并将每个答案保存为唯一变量(我猜测变量的递减)
在我从用户那里获得所有正确的文件名之后,程序将根据视频程序语法调用视频程序(我不需要帮助的语法,我理解那部分)
例如。 (一个“?”表示我不知道该放什么)
@回声关闭
set/p howmany=你要添加多少个文件?
对于/? %%variable(???) in (%howmany%???) do (set/p inputfilename=你要添加的第一个文件的名称是什么? inputfilename=filename set %howmany%-1 ???= %多少%????)
因此,如果用户对问题 1 回答了 5 次,那么 for 循环应该问问题 2 五次,并为每次给出答案创建 5 个唯一变量。 inputfilename1 = movie1.mov inputfilename2 = movie2.mov 等等。
几天来我一直在努力解决这个问题..我无法绕过它。我之前已经为命令做了很多,但这让我很困惑。我的浏览器历史记录充满了谷歌搜索,似乎人们会询问任何类型的文件。如果我确实找到了与这个问题非常接近的任何东西,它总是被要求使用不同的编程语言。我的脑子炸了。这甚至可能吗?请帮助并提前致谢。
最佳答案
虽然 Martin 的回答描述了如何创建唯一变量,但他没有解释如何读取它们。当您谈论“将每个答案保存为唯一变量”时,此处涉及的概念是数组。您需要使用延迟扩展来获取唯一变量(“数组元素”)的值;有关详细信息,请键入 set/?
并查找“delayed expansion”。您可以在这篇文章中阅读有关批处理文件中阵列管理的详细说明:Arrays, linked lists and other data structures in cmd.exe (batch) script
@echo off
setlocal EnableDelayedExpansion
set /p howmany=How many files do you want to add?
for /L %%i in (1,1,%howmany%) do (
set /p inputfilename[%%i]=what is the name of the file you want to add?
)
rem Process array elements (just show them in this case)
for /L %%i in (1,1,%howmany%) do (
echo %%i- !inputfilename[%%i]!
)
下面的示例可以帮助您更轻松地理解数组管理:
@echo off
setlocal EnableDelayedExpansion
rem Create an array of ordinal terms
set i=0
for %%a in (first second third fourth fifth sixth) do (
set /A i+=1
set term[!i!]=%%a
)
rem Previous FOR is equivalent to: set term[1]=first, set term[2]=second, ...
set /p howmany=How many files do you want to add?
for /L %%i in (1,1,%howmany%) do (
set /p inputfilename[%%i]=what is the name of the !term[%%i]! file you want to add?
)
rem Process array elements (just show them in this case)
for /L %%i in (1,1,%howmany%) do (
echo The !term[%%i]! file is !inputfilename[%%i]!
)
关于Windows 命令行脚本根据用户定义的起始变量递减 FOR 循环中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15998163/