PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Stop The Internet Filter!

   No Clean Feed

   The Internet Filter Is An Ex-parrot!






PGTS Humble Blog

Thread: Tips/Tricks For Programming etc

GP JPG
Please, please don't throw me into the briar patch, Brer Fox!

Counting arguments in a windows batch file.


Chronogical Blog Entries:



Date: Thu, 30 Nov 2017 23:00:00 +1100

Counting 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

Other Blog Posts In This Thread:

Copyright     2017, Gerry Patterson. All Rights Reserved.