PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Give Windows The Boot!
   And Say Goodbye To Viruses!

   Ubuntu

   If you own a netbook/laptop~
   Download Ubuntu Netbook!






PGTS Humble Blog

Thread: Tips/Tricks For Programming etc

Author Image 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 +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.