PGTS G. Patterson.   T/A PGTS ABN: 99885392845

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Stop Spam! Stop Viruses!
   Secure And Reliable Ubuntu Desktop!

   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
Throughout my life, my two greatest assets have been mental stability and being, like, really smart .... And a very stable genius at that! -- Donald Trump, 2018.

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.