批量解析每个参数 [英] Batch parse each parameter
问题描述
我正在尝试创建一个批处理脚本,该脚本将对给定的每个参数执行相同的操作.例如,将X文件作为参数:
script.bat "file1.txt" "file2.txt" "file3.txt" "file4.txt" ... "fileX.txt"
将其重命名为:
"file1.bin" "file2.bin" "file3.bin" "file4.bin" ... "fileX.bin"
重命名只是一个例子,对于更复杂的操作,我也将需要它.
我想应该是for each
之类的东西,但是我是批处理脚本的新手.
I am trying to create a batch script which would perform the same action for each parameter given. For example, giving X files as parameters:
script.bat "file1.txt" "file2.txt" "file3.txt" "file4.txt" ... "fileX.txt"
will rename them to:
"file1.bin" "file2.bin" "file3.bin" "file4.bin" ... "fileX.bin"
Rename is just an example, I will need it for more complex operations too.
I guess it should be something like for each
but I'm new in batch scripts.
我只是想知道我是否可以增加%1
索引...
I just wonder if I could just increment %1
index...
推荐答案
您可以使用SHIFT
将参数向左移动.换句话说,调用shift将把第二个参数放到%1,第三个参数放到%2,依此类推.
You can use SHIFT
to shift the parameters left. In other words calling shift will put the second parameter to %1, the third to %2 etc.
所以您需要类似的东西:
So you need something like:
@ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
ECHO %1
SHIFT
GOTO Loop
:Continue
这只会按顺序打印参数,但是您可以在循环内做任何您想做的事情.
This will just print the arguments in order, but you can do whatever you want inside the loop.
这篇关于批量解析每个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!