JSI Tip 10266. How can I count the number of arguments my batch script was invoked with?


There is no standard method for counting the number of arguments that a batch script was invoked with, unlike the count attribute of .VBS arguments.

I have scripted NumbArgs.bat to count the number of arguments that your batch script was invoked with.

The syntax for using NumbArgs.bat is:

Call NumbArgs Numb %*

Where Numb is a call directed numeric environment variable that will contain the number of arguments.

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
)



Hide comments

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.
Publish