Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count amount of parameters sent to batch file [duplicate]

Just converting some shell scripts into batch files and there is one thing I can't seem to find...and that is a simple count of the number of command line arguments.

eg. if you have:

myapp foo bar

In Shell:

  • $# -> 2
  • $* -> foo bar
  • $0 -> myapp
  • $1 -> foo
  • $2 -> bar

In batch

  • ?? -> 2 <---- what command?!
  • %* -> foo bar
  • %0 -> myapp
  • %1 -> foo
  • %2 -> bar

So I've looked around, and either I'm looking in the wrong spot or I'm blind, but I can't seem to find a way to get a count of number of command line arguments passed in.

Is there a command similar to shell's "$#" for batch files?

ps. the closest i've found is to iterate through the %1s and use 'shift', but I need to refernece %1,%2 etc later in the script so that's no good.

like image 479
pyko Avatar asked Nov 19 '22 18:11

pyko


1 Answers

Googling a bit gives you the following result from wikibooks:

set argC=0
for %%x in (%*) do Set /A argC+=1

echo %argC%

Seems like cmd.exe has evolved a bit from the old DOS days :)

like image 119
nimrodm Avatar answered Nov 30 '22 22:11

nimrodm