Search code examples
batch-filedelayedvariableexpansion

batch scripting getting arguments to handle argument plus one


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

Solution

  • 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