|
|
PGTS Humble BlogThread: Tips/Tricks For Programming etc |
|
![]() |
Gerry Patterson. The world's most humble blogger |
Edited and endorsed by PGTS, Home of the world's most humble blogger | |
| |
Counting arguments in a windows batch file. |
|
Chronogical Blog Entries: |
|
| |
Date: Thu, 30 Nov 2017 23:00:00 +1100Counting the Arguments, was something you may want to do in a Windows CMD script |
Here are two ways to do this:
First this rather simple example, uses the for command:
@echo off set argc=0 for %%G in (%*) do set /a "argc = %argc + 1" echo Count is %argc% echo Args are %* goto :eof
Next this rather more convoluted technique calls a subroutine that counts arguments:
@echo off call :count_arg argc %* echo Count is %argc% echo Args are %* goto :eof :: Function :count_arg Arg1 [Arg2] ... [ArgN] :: Arg1 = Variable name (to store the count in) :: Arg2 ... ArgN optional list of zero to N items :count_arg set var_name=%1 set /a "%var_name% = 0" :count_loop if not [%2]==[] ( shift set /a "%var_name% = %var_name% + 1" goto :count_loop ) set var_name= goto :eof