0

I face some problem while trying to print array values as shown below:

@echo off
setlocal EnableDelayedExpansion 
set args=
set /A argc=0
SET /A argn=0
for %%a in (%*) do (
SET args[!argn!]=%%a
SET /A argn+=1
)

FOR %%q in (%*) DO (
echo !args[%argc%]! //not able to print the value
call echo  %%args[!argc!]%%  // this worked

if "%%q" == "--snap" (
      set /A argc+=1
      set SNAP=!args[%argc%]! //this didn't work
)
if "%%q" == "--source" (
      set /A argc+=1
      call SET "SOURCE=%%args[!argc!]%%" //this didn't work too          
)
set /A argc+=1
)

Using this segment of code prints only the first value of the array but the other method of using for /l works fine. How do i correct this? Is it possible to store this array value in any other variable? If so, how?

RRR
  • 81
  • 1
  • 10
  • `for %%q in (%*) do ...` how many parameters does your batch file get from command line?. BTW `set /a argc+=1` is preferred syntax – elzooilogico Nov 21 '16 at 10:27
  • 13 arguments from command line. @elzooilogico – RRR Nov 21 '16 at 10:37
  • and how is the array populated? I don't see how it`s filled? – elzooilogico Nov 21 '16 at 10:45
  • you must use either `call echo %%args[!argc!]%%` or `for %%a in (!argc!) do echo !args[%%a]!` – elzooilogico Nov 21 '16 at 10:52
  • Array is populated as: set /a argn=0 for %%a in (/*) do ( set args[!argn!]=%%a set argn=!argn!+1 ) – RRR Nov 21 '16 at 10:53
  • Thanks. Using call echo %%args[!argc!]%% worked. But how can i now assign this value to any other variable. And how will the second method using (!argc!) work? Do yu mean argc as the total count of array values? @elzooilogico – RRR Nov 21 '16 at 11:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128617/discussion-between-rrr-and-elzooilogico). – RRR Nov 21 '16 at 11:09
  • @RRR, ALWAYS update your code with relevant code within your QUESTION. Not in a comment. Because you are only showing us a portion of your code we cannot assume anything. You have to show us all the code that affects how this portion of your code works. – Squashman Nov 21 '16 at 14:32
  • All array management details are fully explained at [this post](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)... – Aacini Nov 21 '16 at 15:19
  • @Squashman i have edited the code. – RRR Nov 22 '16 at 12:11

2 Answers2

0

You are trying to echo a variable you haven't set, it will therefore be blank.

@Echo Off
SetLocal EnableDelayedExpansion 
Set argc=0
For %%q In (%*) Do (
    Set "args[!argc!]=%%q"
    Rem The next line is for information only   
    Call Echo=%%args[!argc!]%%
    Set/A argc+=1
)
Rem take a look at the variables
Set args[
Timeout -1
Compo
  • 36,585
  • 5
  • 27
  • 39
  • But how do i assign this value to another variable? @Compo – RRR Nov 21 '16 at 11:58
  • I'm not sure which value or what variable you are referring to. _the second parameter can already be referenced using **%2** or **%args[1]%** so I don't really understand why you would want to set this again as another variable_. Unless I know more I cannot really advise. – Compo Nov 21 '16 at 13:02
  • how do i set %%args[!argc!]%% to some other variable? Like, set snap=%%args[!args!]%%. Will this work? – RRR Nov 21 '16 at 13:23
  • Which arg are you wanting to set snap to equal? If you are wanting to set snap to equal all of them then just do that at the top of your script, `Set "snap=%*"`. – Compo Nov 21 '16 at 13:27
0

Assuming delayedexpansion has been invoked and argn has been set to #args+1 as reported in comments,

set /a argn-=1
for /L %%q in (0,1,%argn%) do echo !args[%%q]!
set /a argn+=1

would be my preference.

Magoo
  • 77,302
  • 8
  • 62
  • 84