1

I want to be able to get the next argument to compare to the current argument. So when the argVec is equal to "--define", I want to echo the next argument. I get the result "y" instead of "delivery".

My input is: Cmd version version1 --define delivery

set inputArg=%*
setlocal enabledelayedexpansion
set Count=0
for %%x in (%inputArg%) do (
   set /A Count+=1
   set "argVec[!Count!]=%%~x"
)
for /L %%i in (1,1,%Count%) do echo %%i- !argVec[%%i]!
for /L %%x in (1,1,%Count%) do (
  set /A y=%%x+1
  @echo !y!
  @echo !argVec[%%x]!
  if "!argVec[%%x]!"=="--define" (
    @echo !argVec[!y!]!
  )
)
endlocal
shoover
  • 3,071
  • 1
  • 29
  • 40

2 Answers2

1

When I added @echo off to the top of your script and ran it, I got the following output:

1- version1
2- --define
3- delivery
2
version1
3
--define
y
4
delivery

If I understand you correctly, the problem is the y on the third line from the bottom.

The reason you are getting y is because of @echo !argVec[!y!]!. This tokenizes as @echo, !argVec[!, y, !]!, which means "echo the contents of the !argVec[! variable, then echo y, then echo the contents of the ] variable. Since you don't have an !argVec[! variable or a ] variable, this reduces to "echo y".

To fix it, there is lots of good information at this SO answer. For your purposes, the important part of that post is this:

To get the value of an element when the index change inside FOR/IF enclose the element in double percents and precede the command with call.

Here is a version of your script that I think does what you want it to do:

@echo off
set inputArg=%*
setlocal enabledelayedexpansion
set Count=0
for %%x in (%inputArg%) do (
   set /A Count+=1
   set "argVec[!Count!]=%%~x"
)
for /L %%i in (1,1,%Count%) do echo %%i- !argVec[%%i]!
for /L %%x in (1,1,%Count%) do (
  set /A y=%%x+1
  @echo !y!
  @echo !argVec[%%x]!
  if "!argVec[%%x]!"=="--define" (
    @call echo %%argVec[!y!]%%
  )
)
endlocal

That prints:

1- version1
2- --define
3- delivery
2
version1
3
--define
delivery
4
delivery

I realize that echoing to the screen is probably not your final goal, so when you modify the script to do what you really want it to do, remember to use double percents around the whole "array", exclamation points around the index, and precede your command with call.

For example, if you want to add a compare condition, then set the contents of argVec[y] to a temporary variable from a call, and then use the temporary variable in your if, like this:

@echo off
set inputArg=%*
setlocal enabledelayedexpansion
set Count=0
for %%x in (%inputArg%) do (
   set /A Count+=1
   set "argVec[!Count!]=%%~x"
)
for /L %%i in (1,1,%Count%) do echo %%i- !argVec[%%i]!
for /L %%x in (1,1,%Count%) do (
  set /A y=%%x+1
  @echo !y!
  @echo !argVec[%%x]!
  @call set tmpvar=%%argVec[!y!]%%
  if "!tmpvar!"=="--define" (
    echo "found it"
  )
)
endlocal

Output of the latest:

1- version1
2- --define
3- delivery
2
version1
"found it"
3
--define
4
delivery
Community
  • 1
  • 1
shoover
  • 3,071
  • 1
  • 29
  • 40
  • Thanks, but what if I want to add the compare condition if "%%argVec[!y!]%%"=="delivery" echo true1 I'm not sure how to use the call on there. – Thomas Pham Aug 11 '15 at 22:58
  • @ThomasPham I edited the answer to include a case of comparing the value. It's easiest to set a temporary variable and then use the temp var in your comparison. – shoover Aug 12 '15 at 15:30
0

You can not "nest" delayed expansions this way:

@echo !argVec[!y!]!

There are several ways to solve this problem, that are described here; the most efficient one is this:

for %%y in (!y!) do @echo !argVec[%%y]!

EDIT: Additional request stated in comment solved.

You may use the same method to get the value of argVec[!y!] and use it in any way you wish. For example:

for %%y in (!y!) do if "!argVec[%%y]!"=="delivery" echo true1
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108