1

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?

HereItIs
  • 145
  • 1
  • 4
  • 15
  • You haven't created any arrays, you've set a variable named `%input_image_list%` to a string value `(1 2 3 4 5 6 7 8 9 )`, a variable named `%output_image_list%` to a string value of `(2 3 4 5 6 7 8 9 10)` and a variable named `%next_needed_image%` to a string value of `(002 003 004 005 006 007 008 009 010)`. – Compo Jul 05 '18 at 12:53
  • thanks, so how do i create actual arrays? What I've used above is how most pages have told me to create an array in batch files – HereItIs Jul 05 '18 at 12:56
  • Technically that would be off topic, because it's a request for tutorial/reference information. Also based on the information I've now provided you'd need to completely [rewrite your question](https://stackoverflow.com/posts/51191502/edit) and title, and provide updated code, showing your attempts at the task and an explaination of what happened when you tried them. – Compo Jul 05 '18 at 13:02
  • How is it off topic? The final line in my question is literally 'How can i properly index the list so that each number within it is passed as a parameter?' which is the exact question I'm asking you now. – HereItIs Jul 05 '18 at 13:07
  • Do you have delayed expansion enabled? – aschipfl Jul 05 '18 at 13:33
  • 1
    There are no arrays in Windows command prompt or batch scripting! There are pseudo-arrays, which are nothing but normal environment variables; so `set VAR=Value` and `set ARR[0]=Element` are technically equivalent variables named `VAR` and `ARR[0]`, resp. So writing `set "ARR=(0 1 2 3)"` does not create an array that can be indexed like `!ARR[0]!` or `!ARR[%idx%]!`, it creates just a variable named `ARR` and containing `(0 1 2 3)`. Reference this great post for more details: [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990). – aschipfl Jul 05 '18 at 13:33
  • It is off topic because your question, as specifically asked to me in your comment was 'how do I create actual arrays?' as you can clearly see, that is markedly different from asking about indexing lists, which you don't have! Once you have those lists, _(pseudo arrays)_, you'd have a possible on topic question. – Compo Jul 05 '18 at 14:43

1 Answers1

2

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
  • 2
    Wow! I like the use of [this technique](https://www.dostips.com/forum/viewtopic.php?f=3&t=6429&p=41035#p41035) to create the array elements! **`;)`** – Aacini Jul 05 '18 at 23:09
  • @Aacini Of course all Kudos go to you ;-). I didn't now that link, inserted it into my answer. –  Jul 06 '18 at 06:10