Number of command line arguments?

  • Thread starter Thread starter Angel Tsankov
  • Start date Start date
A

Angel Tsankov

Is there a predefined batch file parameter that gives the number of
command line arguments?
 
Is there a predefined batch file parameter that gives the number of
command line arguments?


No, but:

call NumbArgs Numb %*

where NumbArgs.bat contains:
@echo off
if {%1}=={} @echo Syntax: [call] NumbArgs Numb %%*&goto :EOF
if exist "%TEMP%\NumbArgs.vbs" goto doit
@echo Dim oArgs>"%TEMP%\NumbArgs.vbs"
@echo Set oArgs = WScript.Arguments>>"%TEMP%\NumbArgs.vbs"
@echo Wscript.Echo oArgs.Count>>"%TEMP%\NumbArgs.vbs"
:doit
for /f "Tokens=*" %%n in ('cscript //nologo "%TEMP%\NumbArgs.vbs" %*') do (
set /a %1=%%n - 1
)

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com
 
Angel said:
Is there a predefined batch file parameter that gives the number of
command line arguments?

No, not predefined.

- - - - - - - - begin screen capture WinXP Pro SP2 - - - - - - - -
C:\cmd>demo\GetMultipleParms Now is the time for all good men to come to
the aid of their country while the quick brown fox jumps over the lazy
dog's back.
parm 0=27
parm 1=Now
parm 2=is
parm 3=the
parm 4=time
parm 5=for
parm 6=all
parm 7=good
parm 8=men
parm 9=to
parm 10=come
parm 11=to
parm 12=the
parm 13=aid
parm 14=of
parm 15=their
parm 16=country
parm 17=while
parm 18=the
parm 19=quick
parm 20=brown
parm 21=fox
parm 22=jumps
parm 23=over
parm 24=the
parm 25=lazy
parm 26=dog's
parm 27=back.

C:\cmd>wyllist demo\GetMultipleParms.cmd
==========begin file C:\cmd\demo\GetMultipleParms.cmd ==========
001. @echo off
002. setlocal enabledelayedexpansion
003. set ctr=0
004. :loop
005. set /a ctr+=1
006. set parm%ctr%=%1
007. if [%2]==[] goto :done
008. shift
009. goto :loop
010. :done
011. set parm0=%ctr%
012. for /l %%a in (0,1,%ctr%) do echo/parm %%a=!parm%%a!
==========end file C:\cmd\demo\GetMultipleParms.cmd ==========
- - - - - - - - end screen capture WinXP Pro SP2 - - - - - - - -
 
Angel said:
Is there a predefined batch file parameter that gives the number of
command line arguments?

At least Phil "beat" me to this FAQ solution, but here is what I have

@echo off & setlocal enableextensions enabledelayedexpansion
set argc=0
:_loop
if "%~1"=="" goto _next
set /a argc+=1
set argv[!argc!]=%~1
shift
goto _loop
:_next
::
echo argc=%argc%
for /l %%i in (1,1,%argc%) do echo argv[%%i]=!argv[%%i]!
endlocal & goto :EOF

The output might be
C:\_D\BAS>cmdfaq 1 2 3 "4 5"
argc=4
argv[1]=1
argv[2]=2
argv[3]=3
argv[4]=4 5

Note a catch. After you apply the above, you no longer can access
the original parameters. If you need them later in the batch, you
have to store them at the outset of the batch file like in the above.

All the best, Timo
 
Timo said:
Angel said:
Is there a predefined batch file parameter that gives the number of
command line arguments?


At least Phil "beat" me to this FAQ solution, but here is what I have

@echo off & setlocal enableextensions enabledelayedexpansion
set argc=0
:_loop
if "%~1"=="" goto _next
set /a argc+=1
set argv[!argc!]=%~1
shift
goto _loop
:_next
::
echo argc=%argc%
for /l %%i in (1,1,%argc%) do echo argv[%%i]=!argv[%%i]!
endlocal & goto :EOF

The output might be
C:\_D\BAS>cmdfaq 1 2 3 "4 5"
argc=4
argv[1]=1
argv[2]=2
argv[3]=3
argv[4]=4 5

Note a catch. After you apply the above, you no longer can access
the original parameters. If you need them later in the batch, you
have to store them at the outset of the batch file like in the above.

All the best, Timo

Hi, Timo,

Here's a version that passes the variables up past the 'endlocal':

- - - - - - - - begin screen capture WinXP Pro SP2 - - - - - - - -
C:\cmd>set parm
Environment variable parm not defined

C:\cmd>demo\GetMultipleParms the quick brown fox jumps over the lazy
dog's back.

C:\cmd>for /l %a in (0,1,%parm0%) do @echo parm%a=!parm%a!
parm0=10
parm1=the
parm2=quick
parm3=brown
parm4=fox
parm5=jumps
parm6=over
parm7=the
parm8=lazy
parm9=dog's
parm10=back.

C:\cmd>wyllist demo\GetMultipleParms.cmd
==========begin file C:\cmd\demo\GetMultipleParms.cmd ==========
001. @echo off
002. setlocal enabledelayedexpansion
003. set ctr=0
004. :loop
005. set /a ctr+=1
006. set parm%ctr%=%1
007. if [%2]==[] goto :done
008. shift
009. goto :loop
010. :done
011. set parm0=%ctr%
012. for /l %%a in (0,1,%ctr%) do echo>>%temp%\%~n0.bat set
parm%%a=!parm%%a!
013. endlocal&for %%a in (call del) do %%a %temp%\%~n0.bat
==========end file C:\cmd\demo\GetMultipleParms.cmd ==========
- - - - - - - - end screen capture WinXP Pro SP2 - - - - - - - -
 
Back
Top