Access Item from Array In Windows Batch File within For Loop

谁都会走 提交于 2019-12-24 09:13:32

问题


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

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?


回答1:


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.

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 ...)

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]!
)

Sample output:

> 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


来源:https://stackoverflow.com/questions/51191502/access-item-from-array-in-windows-batch-file-within-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!