JSI Tip 9562. How can my batch script determine the number of parameters?

I have scripted NumbArguments.bat to easily determine the number of parameters passed to your script.

The syntax for using NumbArguments.bat is:

call NumbArguments Parms %*

where Parms is a call directed numeric environment variable that will contain the number of parameters (%*) passed.


if your script (MyScript.bat) contains:
@echo off
@echo %*
call numbarguments num %*
@echo %num%
:: More statements
, typing MyScript AA BB CC DD would echo 4.

NumbArguments.bat contains:

@echo off
if not exist "%TEMP%\NumbArguments.vbs" @echo Wscript.Echo WScript.Arguments.Count>"%TEMP%\NumbArguments.vbs"
for /f "Tokens=*" %%a in ('cscript //nologo "%TEMP%\NumbArguments.vbs" %*') do (
 set /a %1=%%a - 1

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.