0

I have a batch script that reads and prints each line and the line number from a file entry.txt as below.

I have an array called list.

SETLOCAL enabledelayedexpansion

set i=0
set "strc=autorun_"

set list = PROD01 PROD02 PROD03 BangScan

for /f "tokens=*" %%a in (entry.txt) do (
set /a i+=1

::set /a zrrcount = !i!%5
::echo arrc=%%zrrcount
::echo list[%arrc%]

echo counter"%strc%!i!"
echo line=%%a
java pack.Scan %strc%!i! %%a

)
pause

Inside the for loop I want the first element of the array to be printed for the first loop iteration, When the loop runs the second time the second element of the array should be printed and likewise.

Once the array completes i.e when the last item of the array is printed and the loop continues the array must start printing elements from the start i.e from the first element.

Thus for loop run

iteration 1  must print PROD01
iteration 2  must print PROD02
iteration 3  must print PROD03
iteration 4  must print BangScan
iteration 5  must print PROD01
iteration 6  must print PROD02
and so on ...

Note: !i! prints the iteration number of the loop.

I'm new to batch. Any help would be appreciated.

shifahim
  • 127
  • 1
  • 7
  • 19
  • 1
    you don't have an array, but a list. And you didn't define `%list%`, but `%list %`. And you need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). Oh - and don't use `::` inside a loop. Use `REM` instead. `::` has some ugly unexpected behaviour. – Stephan Jun 02 '20 at 06:56
  • I have already `enabledelayedexpansion` as suggested. I was referring to this link for creating array. https://www.tutorialspoint.com/batch_script/batch_script_arrays.htm – shifahim Jun 02 '20 at 07:00
  • 1
    I would ignore that site/page anyhow @shifahim, because I've just taken a look at the code for the first example and the second line in it is wrong, which means that the code just doesn't work. Go ahead copy and paste it into a batch file and try it, I'll guarantee it doesn't produce the output it states it will! You've copied that error into your code above too, you just haven't noticed it yet because you've not yet used the variable `%list%`. – Compo Jun 02 '20 at 08:53
  • 1
    Please take a look at [this question](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script) that has numerous examples of defining, accessing and modifying Arrays. – T3RR0R Jun 02 '20 at 09:43
  • At first you need to learn that batch scripting does not support list nor array data types (that is why I often call them pseudo-lists and pseudo-arrays, resp.). When you read about such in batch scripts they are actually nothing but *normal* environment variables, only their naming and/or treatment is particular, but their values are still just strings. Then you say you are new to batch, so I strongly recommend to stop using such advanced coding techniques for now and to learn the basics first… – aschipfl Jun 02 '20 at 11:11
  • @T3RR0R tried to seek assistance from the link you shared and guess what i get `the syntax of the command is incorrect` error message for `SET "_Arr.Names="Name 1" "Name 2" ... "Name N""` which was one of the answers. I don't have to make batch my skillset just that one of my task does not run from Unix thus my Windows dependency. – shifahim Jun 02 '20 at 12:56
  • @Shifahim The script in that answer works exactly as expected. Either you've incorrectly coppied it, or you've made the mistake of taking from ony the first part of the answer that uses the syntax of single % expansion that's used directly in the command line - Assuming you haven't just tried to incorperate the code in your script without the understanding of how to apply it. If people gave up on things after one try, nothing would ever be achieved. What's the point of asking for help if your not going to heed the advice that comes with it? – T3RR0R Jun 02 '20 at 16:02

0 Answers0