Count the number of parameters sent to a batch file

How to count the number of command line options sent to a batch file inside it. eg. from CMD I call test.bat abcd will be the result 4 , and test.bat abcdefghijklmno be 15 . My current solution:

 @echo off set var=0 if not "%~1"=="" set var=var+1 if not "%~2"=="" set var=var+1 if not "%~3"=="" set var=var+1 if not "%~4"=="" set var=var+1 if not "%~5"=="" set var=var+1 if not "%~6"=="" set var=var+1 if not "%~7"=="" set var=var+1 if not "%~8"=="" set var=var+1 if not "%~9"=="" set var=var+1 echo %var% pause 

This is an ineffective solution, although, at most 9, you can find

Which alternative?

+5
source share

All Articles