Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Two simple, but related questions about batch file args.

50 views
Skip to first unread message

Kenny McCormack

unread,
Sep 29, 2020, 12:16:55 PM9/29/20
to
1) Is there a simple way to get the number of args passed to a batch (.BAT
or .CMD) file? Like $# in Unix.

2) Just out of curiosity, where are the command line args documented in the
DOS/Windows command prompt help system? I know that things like %~xX
are documented in "help for", but what about the really basic stuff,
like %1, %2, %*?

--
"Unattended children will be given an espresso and a free kitten."

Kerr-Mudd,John

unread,
Sep 29, 2020, 1:25:33 PM9/29/20
to
On Tue, 29 Sep 2020 16:16:54 GMT, gaz...@shell.xmission.com (Kenny
McCormack) wrote:

> 1) Is there a simple way to get the number of args passed to a batch
> (.BAT
> or .CMD) file? Like $# in Unix.
>
No, not without going through the parameters. But why do you need it?
'shift' and loop to process each parameter

> 2) Just out of curiosity, where are the command line args documented
> in the
> DOS/Windows command prompt help system? I know that things like
> %~xX are documented in "help for", but what about the really basic
> stuff, like %1, %2, %*?

%0 is the command (as typed), %1 is the 1st parameter, %* is all the
remaining (tail) commandline.

This seems a good point to start.
https://ss64.com/nt/syntax-args.html


--
Bah, and indeed, Humbug.

Kenny McCormack

unread,
Sep 29, 2020, 7:40:39 PM9/29/20
to
In article <XnsAC47BB6F35...@144.76.35.198>,
Kerr-Mudd,John <nots...@127.0.0.1> wrote:
>On Tue, 29 Sep 2020 16:16:54 GMT, gaz...@shell.xmission.com (Kenny
>McCormack) wrote:
>
>> 1) Is there a simple way to get the number of args passed to a batch
>> (.BAT
>> or .CMD) file? Like $# in Unix.
>>
>No, not without going through the parameters. But why do you need it?
>'shift' and loop to process each parameter

Thank you for your answer.

>> 2) Just out of curiosity, where are the command line args documented
>> in the
>> DOS/Windows command prompt help system? I know that things like
>> %~xX are documented in "help for", but what about the really basic
>> stuff, like %1, %2, %*?
>
>%0 is the command (as typed), %1 is the 1st parameter, %* is all the
>remaining (tail) commandline.

Everybody knows that, of course.

>This seems a good point to start.
>https://ss64.com/nt/syntax-args.html

Strange that you have to go out to the Internet to find documentation of
something so obvious. Why isn't it in the basic help system?

--
(Cruz certainly has an odd face) ... it looks like someone sewed pieces of a
waterlogged Reagan mask together at gunpoint ...

http://www.rollingstone.com/politics/news/how-america-made-donald-trump-unstoppable-20160224

Kerr-Mudd,John

unread,
Sep 30, 2020, 5:46:20 AM9/30/20
to
On Tue, 29 Sep 2020 23:40:38 GMT, gaz...@shell.xmission.com (Kenny
'cos 'help' is per command? Perhaps you want a physical DOS manual; It
came in a binder back in DOS 3 & previous, IIRC. Maybe some sadd^w
collector is willing to part with one.
£50+ for a crushed box?!!
https://www.ebay.co.uk/itm/Microsoft-MS-DOS-5-Upgrade-Operating-System-
Retail-Boxed-RARE-3-5-Disks-Manual/173985912918

Or look at bitsavers (HP version, but mostly generic info)

https://www.mirrorservice.org/sites/www.bitsavers.org/pdf/hp/9000
_hpux/softpc/98870-90050_MSDOS_3.3_Reference_Sep89.pdf

chapter 3-3 on.

Herbert Kleebauer

unread,
Sep 30, 2020, 3:18:38 PM9/30/20
to
On 30.09.2020 01:40, Kenny McCormack wrote:

>>> 2) Just out of curiosity, where are the command line args documented
>>> in the
>>> DOS/Windows command prompt help system? I know that things like
>>> %~xX are documented in "help for", but what about the really basic
>>> stuff, like %1, %2, %*?

call /?

>>This seems a good point to start.
>>https://ss64.com/nt/syntax-args.html

> Strange that you have to go out to the Internet to find documentation of
> something so obvious. Why isn't it in the basic help system?

https://www.microsoft.com/en-us/download/details.aspx?id=56846

Kenny McCormack

unread,
Sep 30, 2020, 4:35:09 PM9/30/20
to
In article <rl2lm6$1nt5$1...@gioia.aioe.org>,
Herbert Kleebauer <kl...@unibwm.de> wrote:
>On 30.09.2020 01:40, Kenny McCormack wrote:
>
>>>> 2) Just out of curiosity, where are the command line args documented
>>>> in the
>>>> DOS/Windows command prompt help system? I know that things like
>>>> %~xX are documented in "help for", but what about the really basic
>>>> stuff, like %1, %2, %*?
>
>call /?

Thank you! That's exactly what I was looking for.

And just to be clear: I know all this stuff - I am not looking for help
with batch file programming. I am looking for help in finding it in the
help system. Now I know that.

>https://www.microsoft.com/en-us/download/details.aspx?id=56846

Strange that you have to go out to the Internet to find documentation of
something so obvious. Why isn't it in the basic help system?

Anyway, I didn't find anything useful at that URL.

P.S. The original question was: Is there a variable that expands to the #
of parameters passed. The answer to that is now known to be: NO.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/TedCruz

Zaidy036

unread,
Sep 30, 2020, 5:04:05 PM9/30/20
to
On 9/29/2020 12:16 PM, Kenny McCormack wrote:
> 1) Is there a simple way to get the number of args passed to a batch (.BAT
> or .CMD) file? Like $# in Unix.
>
> 2) Just out of curiosity, where are the command line args documented in the
> DOS/Windows command prompt help system? I know that things like %~xX
> are documented in "help for", but what about the really basic stuff,
> like %1, %2, %*?
>
I do not know Unix but something like the following will let you, by
changing the cmds in FINDSTR, set a variable name which could be a
counter. %%~AI may also need to be changed depending on type of variable:

FOR %%F IN (%1 %2 %3 %4 %5 %6 %7 %8 %9) DO (
FOR %%I IN (%%F) DO ECHO %%~AI > F:\_ATTRIB.TXT
FINDSTR r F:\_ATTRIB.TXT && ATTRIB -R %%F || ATTRIB +R %%F
)

JJ

unread,
Sep 30, 2020, 5:48:17 PM9/30/20
to
On Wed, 30 Sep 2020 20:35:08 -0000 (UTC), Kenny McCormack wrote:
>
> Strange that you have to go out to the Internet to find documentation of
> something so obvious. Why isn't it in the basic help system?

IMO, it's because CMD is the successor for MS-DOS v5.0 COMMAND.COM, and
COMMAND.COM initially doesn't have any help system. So everything has to be
explained from a printed document. Not even for `COMMAND /?`.

Help system were only added since MS-DOS v5.0, and it was a minimal help
system. Explaining mainly for the command syntax and any of its paramaters.
Further details were added into the help system on each newer COMMAND/CMD
versions, but they still don't cover all aspects of the command interpreter
program.

I could only guess that the developers are lazy. Because the fact that the
help in `CMD /?` doesn't thoroughly explain about the command line itself.
e.g. keyboard shortcut keys for command history, and command/file
completion. Including command grouping, redirection, command separator, and
conditional commands.

Those information were only exist in Windows XP offline help documentation
(in `ntcmds.chm` file). Later Windows versions no longer have such offline
documentation. Instead, it was moved to the internet and was made difficult
to find. Current Microsoft product's internet based documentation system is
a community based documentation. But the fact that all of its products are
proprietary, makes it no sense, considering that no one knows better than
Microsoft themselves. It's ridiculous.

I've researched all CMD versions starting from Windows NT 3.51 to Windows 10
build 1909. There were no major additions in the help system since Windows
Vista. Only minor additions were found in Windows 7, and Windows 10 build
1903. I haven't checked Windows 10 build 2004, but I doubt it has any
addition at all.

Herbert Kleebauer

unread,
Sep 30, 2020, 6:16:56 PM9/30/20
to
On 30.09.2020 22:35, Kenny McCormack wrote:

>>https://www.microsoft.com/en-us/download/details.aspx?id=56846
>
> Strange that you have to go out to the Internet to find documentation of
> something so obvious. Why isn't it in the basic help system?
>
> Anyway, I didn't find anything useful at that URL.

It is a link to ws-commands.pdf (948 pages) with the explanation
of the Windows Commands. Just search for "%1" and you will get the
description for the CALL command.



> P.S. The original question was: Is there a variable that expands to the #
> of parameters passed. The answer to that is now known to be: NO.

But you can calculate it:


@echo off
set nargs=0&call :nargs %*
echo batch called with %nargs% parameters: %*
goto :eof

:nargs
if [%1]==[] (goto :eof) else (shift & set /a nargs+=1 & goto :nargs)

Kenny McCormack

unread,
Oct 4, 2020, 8:16:00 AM10/4/20
to
In article <rl304j$au9$1...@gioia.aioe.org>,
Herbert Kleebauer <kl...@unibwm.de> wrote:
...
>> P.S. The original question was: Is there a variable that expands to the #
>> of parameters passed. The answer to that is now known to be: NO.
>
>But you can calculate it:
>
>
>@echo off
>set nargs=0&call :nargs %*
>echo batch called with %nargs% parameters: %*
>goto :eof
>
>:nargs
>if [%1]==[] (goto :eof) else (shift & set /a nargs+=1 & goto :nargs)
>

This is nice; thanks. I actually ended up going with a version of this.

BTW, particularly cute is the lack of a space between the 0 and the &.
I assumed that I could put a space there (for "readability"), but then
found out why it is the way it is...

I did put a space after the &, though.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/EternalFlame

Herbert Kleebauer

unread,
Oct 4, 2020, 1:24:02 PM10/4/20
to
On 04.10.2020 14:15, Kenny McCormack wrote:

>>set nargs=0&call :nargs %*

> BTW, particularly cute is the lack of a space between the 0 and the &.
> I assumed that I could put a space there (for "readability"), but then
> found out why it is the way it is...
>
> I did put a space after the &, though.

You can use:

set /a nargs=0 &call :nargs %*


Kenny McCormack

unread,
Oct 4, 2020, 1:27:45 PM10/4/20
to
In article <rld0fe$129j$1...@gioia.aioe.org>,
Yeah, I think I like that better. I'd write it as:

set /a nargs=0 & call :nargs %*

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/Cancer

Mike Sanders

unread,
Oct 8, 2020, 8:36:21 PM10/8/20
to
Kenny McCormack writes:

>1) Is there a simple way to get the number of args passed to a batch (.BAT
> or .CMD) file? Like $# in Unix.

How about:

@echo off

set argc=0
for %%x in (%*) do set /a argc+=1

echo %argc%


Mike Sanders

unread,
Oct 8, 2020, 8:51:10 PM10/8/20
to
I see its been answered upthread already, so...


Kenny McCormack

unread,
Oct 9, 2020, 8:17:38 AM10/9/20
to
In article <20201009-0...@Mike-Sanders.news.eternal-september.org>,
Sure, but that's not the point.

Obviously, I was asking if there was a built-in variable, like there is in Unix.
And the answer is: No.

The real underlying is that I find this strange. Given that the DOS batch
language was patterned after Unix shell (in fact, all of DOS was patterned
after Unix), it is odd that they didn't put in such a variable.

P.S. I see that your way is somewhat simpler than the "call nargs" method
the other poster suggested. I wonder if it (the method shown above) works
when there are spaces in the args.

--
Religion is what keeps the poor from murdering the rich.

- Napoleon Bonaparte -

Mike Sanders

unread,
Oct 9, 2020, 2:19:20 PM10/9/20
to
Kenny McCormack writes:
>In article <20201009-0...@Mike-Sanders.news.eternal-september.org>,
>Mike Sanders <mi...@porkchop.bsd> wrote:
>>Kenny McCormack writes:
>>
>>>1) Is there a simple way to get the number of args passed to a batch
(.BAT
>>> or .CMD) file? Like $# in Unix.
>>
>>How about:
>>
>>@echo off
>>
>>set argc=0
>>for %%x in (%*) do set /a argc+=1
>>
>>echo %argc%
>
>Sure, but that's not the point.
>
>Obviously, I was asking if there was a built-in variable, like there is in
Unix.
>And the answer is: No.

[thinking to myself: umm, hyperbole from an otherwise humble guy...
doesnt suit him so well]

>The real underlying is that I find this strange. Given that the DOS batch
>language was patterned after Unix shell (in fact, all of DOS was patterned
>after Unix), it is odd that they didn't put in such a variable.

%COMSPEC% < $SHELL -- A pale imitation by Microsoft if ever there was. No
doubt you've experienced it.

>P.S. I see that your way is somewhat simpler than the "call nargs" method
>the other poster suggested. I wonder if it (the method shown above) works
>when there are spaces in the args.

As written...

3 args: foo.cmd x y z

2 args: foo.cmd x "y z"

--
Mike Sanders
echo zv...@ohflobk.ulcrezneg.arg | rot13
https://busybox.hypermart.net

Timo Salmi

unread,
Oct 24, 2020, 11:22:19 AM10/24/20
to
On 29.09.2020 19:16, Kenny McCormack wrote:
> 1) Is there a simple way to get the number of args passed to a batch (.BAT
> or .CMD) file? Like $# in Unix.
>
> 2) Just out of curiosity, where are the command line args documented in the
> DOS/Windows command prompt help system? I know that things like %~xX
> are documented in "help for", but what about the really basic stuff,
> like %1, %2, %*?

40} How to get the number of and parse the arguments given to a script?
http://www.elisanet.fi/tsalmi/info/tscmd040.html


All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
http://www.elisanet.fi/tsalmi/homepage.html
Useful CMD script tricks http://www.elisanet.fi/tsalmi/info/tscmd.html

mokomoji

unread,
Oct 25, 2020, 1:49:28 PM10/25/20
to
2020년 9월 30일 수요일 오전 1시 16분 55초 UTC+9에 Kenny McCormack님이 작성한 내용:
You will learn from this topic.
String parameter
Numeric parameter
What if you can't learn?
It has a brain structure similar to mine.


1) SAMPLE
All transformable basic sources for your "parameters"

SOURCE
@echo off
setlocal
cd /d "%~dp0
call :z_shift a b c d e f g h i j k l m n o p
goto :end
:z_shift
echo %*
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
goto :eof
:end
endlocal
pause

OUTPUT
a b c d e f g h i j k l m n o p
a b c d e f g h i
계속하려면 아무 키나 누르십시오 . . .

1-1) Application example
@echo off
setlocal
cd /d "%~dp0
call :z_shift a b c d e f g h i j k l m n o p
echo parameters total = %z_nargs%
goto :end
:z_shift
echo batch called with %%z_nargs%% parameters:
echo %*
echo.
set z_nargs=0
:z_loop
if /i "%1" neq "" (
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
shift & set /a z_nargs+=1 & goto :z_loop)||goto :eof
goto :eof
:end
endlocal
pause

OUTPUT
batch called with %z_nargs% parameters:
a b c d e f g h i j k l m n o p

a b c d e f g h i
b c d e f g h i j
c d e f g h i j k
d e f g h i j k l
e f g h i j k l m
f g h i j k l m n
g h i j k l m n o
h i j k l m n o p
i j k l m n o p
j k l m n o p
k l m n o p
l m n o p
m n o p
n o p
o p
p
parameters total = 16
계속하려면 아무 키나 누르십시오 . . .


2) parameter documentd make~!!
Do it now! "PARAMETER" complete conquest

@echo off
setlocal
cd /d "%~dp0">para_doc.txt
echo -------------------------------------->>para_doc.txt
echo [Parameter Documentd]>>para_doc.txt
echo -------------------------------------->>para_doc.txt
echo.>>para_doc.txt
echo.>>para_doc.txt
echo.>>para_doc.txt
echo -------------------------------------->>para_doc.txt
echo shift -------->>para_doc.txt
echo -------------------------------------->>para_doc.txt
shift /?>>para_doc.txt
echo.>>para_doc.txt
echo.>>para_doc.txt
echo -------------------------------------->>para_doc.txt
echo call -------->>para_doc.txt
echo -------------------------------------->>para_doc.txt
call /?>>para_doc.txt
echo.>>para_doc.txt
echo.>>para_doc.txt
echo -------------------------------------->>para_doc.txt
echo for -------->>para_doc.txt
echo -------------------------------------->>para_doc.txt
for /?>>para_doc.txt
echo.>>para_doc.txt
echo.>>para_doc.txt
echo -------------------------------------->>para_doc.txt
echo ETC use :--------->>para_doc.txt
echo -------------------------------------->>para_doc.txt
echo etc use commad : cd,md,rd,pushd,popd,cmd,start,:>>para_doc.txt
echo copy type command,comp type command:>>para_doc.txt
endlocal


0 new messages