在For Loop中从Windows批处理文件中的数组访问项目 [英] Access Item from Array In Windows Batch File within For Loop

查看:64
本文介绍了在For Loop中从Windows批处理文件中的数组访问项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同列表中的不同数字作为每次迭代中的参数来迭代Windows批处理文件中的列表.

Im trying to iterate over a list in windows batch file, using the the different numbers within different lists as parameters in each iteration.

set input_image_list=(1 2 3 4 5 6 7 8 9 )

set output_image_list = (2 3 4 5 6 7 8 9 10)

set next_needed_image = (002 003 004 005 006 007 008 009 010)

FOR %%A IN (2 3 4 5 6 7 8 9 10)  DO python batchLayer4ThirdVideo.py --input_image !input_image_list[%%A]! --next_undreamed_image !next_needed_image[%%A]! --output_image %%A

但是当我使用列表作为参数时对列表的索引不起作用,它只是将索引例如input_image_list[2]作为我的参数,当它应该只是一个数字时.

but the indexing of the lists when i use them as parameters doesnt work, it just puts the index e.g input_image_list[2] as my parameter, when it should just be a number.

我如何正确索引列表,以便将其中的每个数字作为参数传递?

How can i properly index the list so that each number within it is passed as a parameter?

推荐答案

有一种(以某种方式令人不安的想法)

There is a (somehow mind twisting) technique to expand string lists to arrays by replacing the spaces (or any other char) between the list elements with a count and a set to form the array.

从列表和列表中尚不清楚,是要基于0还是1的索引.
(更容易的是for /l %%A in (2,1,10) Do ...)

From your lists and the for it is unclear if you want a 0 or 1 based index.
(more easy would be a for /l %%A in (2,1,10) Do ...)

为了更好地理解,我使用了较短的变量名,并将已拆分的python命令回显到多行.

For better understanding I used shorter variable names and echo the split'ed python command to several lines.

:: Q:\Test\2018\07\05\SO_51191502.cmd
@Echo off & SetLocal EnableDelayedExpansion

set i=0&Set "ImgIn= 1 2 3 4 5 6 7 8 9 10"
Set "ImgIn=%ImgIn: ="&Set /a i+=1&Set "ImgIn[!i!]=%"
:: Set ImgIn

set i=0&Set "ImgOut= 2 3 4 5 6 7 8 9 10 11"
Set "ImgOut=%ImgOut: ="&Set /a i+=1&Set "ImgOut[!i!]=%"
:: Set ImgOut

set i=0&Set "ImgNext= 002 003 004 005 006 007 008 009 010 011"
Set "ImgNext=%ImgNext: ="&Set /a i+=1&Set "ImgNext[!i!]=%"
:: Set ImgNext

For /L %%A IN (1,1,10) DO (
 Echo python batchLayer4ThirdVideo.py ^
 --input_image !ImgIn[%%A]! ^
 --next_undreamed_image !ImgNext[%%A]! ^
 --output_image !ImgOut[%%A]!
)

示例输出:

> Q:\Test\2018\07\05\SO_51191502.cmd
python batchLayer4ThirdVideo.py  --input_image 1  --next_undreamed_image 002  --output_image 2
python batchLayer4ThirdVideo.py  --input_image 2  --next_undreamed_image 003  --output_image 3
python batchLayer4ThirdVideo.py  --input_image 3  --next_undreamed_image 004  --output_image 4
python batchLayer4ThirdVideo.py  --input_image 4  --next_undreamed_image 005  --output_image 5
python batchLayer4ThirdVideo.py  --input_image 5  --next_undreamed_image 006  --output_image 6
python batchLayer4ThirdVideo.py  --input_image 6  --next_undreamed_image 007  --output_image 7
python batchLayer4ThirdVideo.py  --input_image 7  --next_undreamed_image 008  --output_image 8
python batchLayer4ThirdVideo.py  --input_image 8  --next_undreamed_image 009  --output_image 9
python batchLayer4ThirdVideo.py  --input_image 9  --next_undreamed_image 010  --output_image 10
python batchLayer4ThirdVideo.py  --input_image 10  --next_undreamed_image 011  --output_image 11

这篇关于在For Loop中从Windows批处理文件中的数组访问项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆