0

I'm new to using batch files. I'm trying to run a program I've written several times with slightly different command line parameters. In order to do this, I've made two lists that hold the different parameters. I then want to iterate through both lists, but I'm getting really confused on how to get the items from my list in a loop. (Looking through batch tutorials and other threads hasn't helped).

My batch code is as follows:

set D=("airplanebase.xml","catfacebase.xml","churchbase.xml","coffee_cupbase.xml","hutbase.xml","maskbase.xml","pumpkinbase.xml","spacebase.xml")
set C=("186.svg","187.svg","188.svg","189.svg","190.svg","191.svg","192.svg","193.svg")
FOR /l %%A IN (0,1,7) DO (
CurvedNonogramsGenerator.exe file=%D[%%A]% outfile=%C[%%A]% -af -am cratio="0" outdir="\output\\" weights="1000,3,3,7,0" temp="1.001" pairits="0"
)

When I try to run it the echo says that it's calling my program like this:

CurvedNonogramsGenerator.exe file= outfile= -af -am cratio="0" outdir="C:\Users\DeMeessias\Documents\1. Studie\0.MasterThesis\output\\" weights="1000,3,3,7,0" temp="1.001" pairits="0"

It also gives an error that says the index is out of the bounds of the matrix. As you can see the two arguments that I'm trying to get from the lists are left blank.

I'm just really confused with how the % operator works I guess. I've also seen tutorials that use ! as an operator for getting stuff out of lists, but that doesnt seem to work either. It is treated as though the ! is just random text rather than an operator.

So how do I get it to correctly load in the arguments from the lists?

Edit: So following advice from comments I changed the batch file to:

set D[0]="airplanebase.xml"
set D[1]="catfacebase.xml"
set D[2]="churchbase.xml"
set D[3]="coffee_cupbase.xml"
set D[4]="hutbase.xml"
set D[5]="maskbase.xml"
set D[7]="pumpkinbase.xml"
set D[8]="spacebase.xml"
set C[0]="186.svg"
set C[1]="187.svg"
set C[2]="188.svg"
set C[3]="189.svg"
set C[4]="190.svg"
set C[5]="191.svg"
set C[6]="192.svg"
set C[7]="193.svg"
FOR /l %%A IN (0,1,7) DO (
CurvedNonogramsGenerator.exe file=!D[%%A]! outfile=!C[%%A]! -af -am cratio="0" outdir="C:\Users\DeMeessias\Documents\1. Studie\0.MasterThesis\output\\" weights="1000,3,3,7,0" temp="1.001" pairits="0"
)

But this doesnt solve the problem either... The echo now says that its calling my program like

CurvedNonogramsGenerator.exe file=!D[0]! outfile=!C[0]! -af -am cratio="0" outdir="C:\Users\DeMeessias\Documents\1. Studie\0.MasterThesis\output\\" weights="1000,3,3,7,0" temp="1.001" pairits="0"

Which gives the index out of bounds error again...

EDIT 2: Adding setlocal EnableDelayedExpansion to the top of the file fixed it! Thanks for the help everyone.

DeMeessias
  • 27
  • 5
  • Use [delayed expansion](http://ss64.com/nt/delayedexpansion.html) for the variables `D` and `C`, so `!D[%%A]!`, for instance... – aschipfl Sep 21 '17 at 17:54
  • 1
    Until you have set variables to `%D[0]%`,`%D[1]%`,`%D[2]%`,`%D[3]%`,`%D[4]%`,`%D[5]%`,`%D[6]%`,`%D[7]%`,`%C[0]%`,`%C[1]%`,`%C[2]%`,`%C[3]%`,`%C[4]%`,`%C[5]%`,`%C[6]%` & `%C[7]%` enabling delayed expansion will not help you though! – Compo Sep 21 '17 at 18:00
  • Batch files no nothing about arrays or lists. Everything is a string unless you are using `set /a` to do an arithmetic operation. – Squashman Sep 21 '17 at 20:58

1 Answers1

1

There are several ways to solve this problem. The method below is a simple one:

@echo off
setlocal EnableDelayedExpansion

set D="airplanebase.xml","catfacebase.xml","churchbase.xml","coffee_cupbase.xml","hutbase.xml","maskbase.xml","pumpkinbase.xml","spacebase.xml"
set C="186.svg","187.svg","188.svg","189.svg","190.svg","191.svg","192.svg","193.svg"

for %%C in (%C%) do (
   for /F "tokens=1* delims=," %%D in ("!D!") do (
      ECHO CurvedNonogramsGenerator.exe file=%%D outfile=%%C -af -am cratio="0" outdir="\output\\" weights="1000,3,3,7,0" temp="1.001" pairits="0"
      set "D=%%E"
   )
)

You may also convert the lists into arrays, so your original method could be used; that is:

@echo off
setlocal EnableDelayedExpansion

set D="airplanebase.xml","catfacebase.xml","churchbase.xml","coffee_cupbase.xml","hutbase.xml","maskbase.xml","pumpkinbase.xml","spacebase.xml"
set C="186.svg","187.svg","188.svg","189.svg","190.svg","191.svg","192.svg","193.svg"

rem Convert the lists into arrays
set i=0
for %%D in (%D%) do (
   set /A i+=1
   set "D[!i!]=%%D"
)
set i=0
for %%C in (%C%) do (
   set /A i+=1
   set "C[!i!]=%%C"
)

for /L %%A in (1,1,%i%) do (
   ECHO CurvedNonogramsGenerator.exe file=!D[%%A]! outfile=!C[%%A]! -af -am cratio="0" outdir="\output\\" weights="1000,3,3,7,0" temp="1.001" pairits="0"
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Did you realized that I posted this answer before you modified the question? If this answer is useful to you, it is a good practice to accept it via the green check-mark. If this answer is not what you wanted, then I'll appreciate it if you could indicate what is the problem with it... Thanks! – Aacini Sep 24 '17 at 22:36
  • Sorry, still new to actually posting my own questions on stackexchange rather than just skimming other peoples topics. Your answer did help me seeing as I got the idea to enable EnableDelayedExpansion from you. Thanks! – DeMeessias Sep 25 '17 at 08:39