1

Please check below code and correct. Thanks in advance.

start test.bat A B C D E F G H

set argCount=0

for  %%x in (%*) do (

set /A argCount+=1   
    if %argCount% gtr 3 (

    echo element after 3 argument %%x   
    )
 )

echo Number of processed arguments: %argCount%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    The `shift` command is used for parameters greater than 9. See `shift /?`. Basically do a loop until nth parameter is blank, issuing a `shift` each time. –  Jun 05 '16 at 06:49
  • http://stackoverflow.com/questions/19835849/batch-script-iterate-through-arguments/19837690#19837690 – Aacini Jun 05 '16 at 21:03

2 Answers2

0

You need delayed expansion:

setlocal enableDelayedExpansion
set argCount=0

for  %%x in (%*) do (
    set /A argCount+=1   
    if !argCount! gtr 3 (
     echo element after 3 argument %%x   
    )
 )
echo Number of processed arguments: %argCount%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

Wow! Original npocmaka's code does not work!

==> D:\bat\SO\37639143npocmaka.bat Wow! Original npocmaka's code does not work!
Number of processed arguments: 1

You need to toggle delayed expansion as follows:

SETLOCAL EnableExtensions DisableDelayedExpansion

set argCount=0

for  %%x in (%*) do (
    set /A argCount+=1
    setlocal EnableDelayedExpansion   
    if !argCount! gtr 3 (
       endlocal
       echo element after 3 argument %%x   
    ) else endlocal
 )
echo Number of processed arguments: %argCount%

Output:

==> D:\bat\SO\37639143.bat Wow! Original npocmaka's code does not work!
element after 3 argument code
element after 3 argument does
element after 3 argument not
element after 3 argument work!
Number of processed arguments: 7

Or, omit delayed expansion at all and modify next code snippet (my unchanged specimen script):

@echo OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
echo(
echo  before any shift:  all %%* = [%*]
echo(
set /A "ii=0"
  echo param %%%ii% = %0
:loopfor
  set /A "ii+=1"
  echo param %%%ii% = %1
  SHIFT /1
  if not [%1]==[] goto :loopfor
echo(
echo  after %ii% shifts: %%* = [%*]
goto :eof

Output:

==> cliparser.bat Wow! Original npocmaka's code does not work!

 before any shift:  all %* = [Wow! Original npocmaka's code does not work!]

param %0 = cliparser.bat
param %1 = Wow!
param %2 = Original
param %3 = npocmaka's
param %4 = code
param %5 = does
param %6 = not
param %7 = work!

 after 7 shifts: %* = [Wow! Original npocmaka's code does not work!]

==>
Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • 1
    npocmaka's code works absolutely fine, but with a limitation that arguments containing `!` will be corrupted. Your "fix" of toggling delayed expansion accomplishes nothing. The proper fix is to also save the FOR variable in an environment variable before enabling delayed expansion, and then reference the env. variable with delayed expansion. But I never use a FOR loop to iterate parameters because wildcards `*` and `?` will be expanded. Instead I always use a GOTO loop with SHIFT - similar to your second code. – dbenham Jun 05 '16 at 19:18