Discussion:
Use the remaining arguments ?
(too old to reply)
R.Wieser
2024-04-22 15:16:03 UTC
Permalink
Hello all,

I've got a batchfile in which I use the first (%1) argument as a switch to
decide which program should be started with the remaining arguments.

The problem is that I can't seem to find how to pass the unknown number
of(!) remaining arguments to the called program.

I'm aware of "%*", which stands for "all arguments but %0", but when I use
SHIFT (to get rid of "%1" it doesn't change.

iow:

echo %*
shift
echo %*

shows the same on both lines.

Question:
How do I provide all arguments starting from the second one to the program ?

Currently I just use "program.exe %2 %3 %4 %5 %6 %7 %8 %9" and be done with
it, but would like to know a way to provide more than 9 arguments to the
batchfile and still have it work.

Regards,
Rudy Wieser
Kenny McCormack
2024-04-22 16:14:27 UTC
Permalink
Post by R.Wieser
Hello all,
I've got a batchfile in which I use the first (%1) argument as a switch to
decide which program should be started with the remaining arguments.
...
Post by R.Wieser
How do I provide all arguments starting from the second one to the program ?
Currently I just use "program.exe %2 %3 %4 %5 %6 %7 %8 %9" and be done with
it, but would like to know a way to provide more than 9 arguments to the
batchfile and still have it work.
I've hit this myself, long ago. I think it is just a limitation of DOS
batch that we all have to resign ourselves to living with.

There are quite a few alternatvies to plain DOS batch at this point in
time, so you could switch languages. I know this isn't what you want to
hear, of course.

In particular, it is amazing how much better of a scripting language bash
is, compared to plain DOS batch. You could write in bash and run it under
Cygwin (or similar)...

Or you could do something like:

set prog=%1
shift
set args=
:loop
if not %1X==X (
set args=%args% %1
shift
goto loop
)
%prog% %args%
--
When polled for who is/was the worst US president, the candidates (i.e. popular responses/suggestions) are usually:
(Andrew) Johnson, Buchanan, or Trump
When polled for who is/was the best US president, the candidates (i.e. popular responses/suggestions) are usually:
FDR, Lincoln, or Trump
R.Wieser
2024-04-22 17:33:31 UTC
Permalink
Kenny,
Post by R.Wieser
How do I provide all arguments starting from the second one to the program ?
Currently I just use "program.exe %2 %3 %4 %5 %6 %7 %8 %9" and be
done with it,
I think it is just a limitation of DOS batch that we all have to
resign ourselves to living with.
:-) That is what I am trying to figure out : /is/ it a limitation, or is
there something available I'm currently not aware of.

To be honest, I consider %* not honoring SHIFT to be a bug, greatly
diminishing the usefullness of it.
There are quite a few alternatvies to plain DOS batch at this point
in time, so you could switch languages.
Batch is just too handly to me for basic stuff, especially when you want to
have everything running in the same console (instead of having each
executable in the script run in its own shell window.)
[snip script]

I was considering something of the same, but in the past found batch rather
finicky when handling strings. Hence I'd rather do as little as possible of
it

Thanks for the suggestion though.

Regards,
Rudy Wieser
Herbert Kleebauer
2024-04-22 16:38:16 UTC
Permalink
Post by R.Wieser
Hello all,
I've got a batchfile in which I use the first (%1) argument as a switch to
decide which program should be started with the remaining arguments.
Currently I just use "program.exe %2 %3 %4 %5 %6 %7 %8 %9" and be done with
it, but would like to know a way to provide more than 9 arguments to the
batchfile and still have it work.
Why not use the program name itself as first parameter,
the you only need to use:

%*

to execute the program with the remaining parameters.

Or if you use numbers (or names) as first parameter to specify
the program you can use something like this:

call :%*
goto :eof

:1
echo program 1 was called with parameter %*
goto :eof

:2
echo program 2 was called with parameter %*
goto :eof

:3
echo program 3 was called with parameter %*
goto :eof
R.Wieser
2024-04-22 17:45:50 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
Why not use the program name itself as first parameter,
[snip]

:-) In that case I would not even bother to prefix the name of the
batchfile to that commandline.
Post by Herbert Kleebauer
Or if you use numbers (or names) as first parameter to specify the program
[snip]

Besides it throwing an error when an unknown first argument would be
provided, it also doesn't do what I asked :

calling it with "1 foo" would display "program 1 was called with parameter 1
foo". Its exactly that last "1" that I'm trying to get rid of.

Regards,
Rudy Wieser
Herbert Kleebauer
2024-04-22 19:36:59 UTC
Permalink
Post by R.Wieser
Herbert,
Post by Herbert Kleebauer
Why not use the program name itself as first parameter,
[snip]
:-) In that case I would not even bother to prefix the name of the
batchfile to that commandline.
I suppose you do some other task in the batch before you
start the program with %*. Or tell us, what exactly you want
to do.
Post by R.Wieser
Post by Herbert Kleebauer
Or if you use numbers (or names) as first parameter to specify the program
[snip]
Besides it throwing an error when an unknown first argument would be
But exactly that should a program do: if you give an illegal input,
it should give an error.
Post by R.Wieser
calling it with "1 foo" would display "program 1 was called with parameter 1
foo". Its exactly that last "1" that I'm trying to get rid of.
Here I get:
program 1 was called with parameter foo

The 1 is removed from the parameters because it is used as label
for the call statement.
R.Wieser
2024-04-22 20:28:26 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
I suppose you do some other task in the batch before you
start the program with %*.
No, I don't. Checking that %1 argument is, part from the "@echo off" the
first line in the script.
Post by Herbert Kleebauer
Or tell us, what exactly you want to do.
Would it make a difference if I told you that the exact "first" line is "if
exist %1.ext goto DoExt" ?
Post by Herbert Kleebauer
Post by R.Wieser
Besides it throwing an error when an unknown first argument would be
But exactly that should a program do: if you give an illegal input,
it should give an error.
It should. But there is quite a bit of difference between the crude error
the OS spew out and a much nicer "hey dumbass, check your input and try
again." message, don't you think ? :-)
Post by Herbert Kleebauer
program 1 was called with parameter foo
The 1 is removed from the parameters because it is used as label
for the call statement.
My apologies. I didn't test it and now I did I see eyou are indeed right.

I think I took that "call" as a "goto" ... :-|
Post by Herbert Kleebauer
The 1 is removed from the parameters because it is used as label
for the call statement.
Yep. I tried, for good measure, "call" as well as "goto" and that was
indeed the difference.

Regards,
Rudy Wieser
Herbert Kleebauer
2024-04-22 21:37:01 UTC
Permalink
Post by R.Wieser
Post by Herbert Kleebauer
Or tell us, what exactly you want to do.
Would it make a difference if I told you that the exact "first" line is "if
exist %1.ext goto DoExt" ?
Yes.

If there are no poisoned characters like %^!& in the arguments,
this should work:

set p=&for %%i in (%*) do if defined p (call set p=%%p%% %%i) else (set p= )
echo %p%
R.Wieser
2024-04-23 08:40:48 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
Post by R.Wieser
Would it make a difference if I told you that the exact "first" line is "if
exist %1.ext goto DoExt" ?
Yes.
Ah. And what would that difference be ?

Mind you, your "call :%*" was quite nifty, but /very/ limited in its usage.
Post by Herbert Kleebauer
If there are no poisoned characters like %^!& in the arguments,
Thats why I called batch'es string-handling finicky. :-|

Its even worse worse : "^" and "%" collapse on the commandline, but only "^"
seems to collapse in each loop-step (I didn't really try the others). And
both solutions you provided respond differently to the usage of the "^"
escape character.
Post by Herbert Kleebauer
set p=&for %%i in (%*) do if defined p (call set p=%%p%% %%i) else (set p= )
echo %p%
Thanks. I never saw that "if defined p" before, so I've learned something
new. :-)

I think I have to conclude that removing the first argument (or more) from a
commandline isn't possible. Not in a dependable way.

Regards,
Rudy Wieser

Zaidy036
2024-04-22 20:29:15 UTC
Permalink
Post by R.Wieser
Hello all,
I've got a batchfile in which I use the first (%1) argument as a switch to
decide which program should be started with the remaining arguments.
The problem is that I can't seem to find how to pass the unknown number
of(!) remaining arguments to the called program.
I'm aware of "%*", which stands for "all arguments but %0", but when I use
SHIFT (to get rid of "%1" it doesn't change.
echo %*
shift
echo %*
shows the same on both lines.
How do I provide all arguments starting from the second one to the program ?
Currently I just use "program.exe %2 %3 %4 %5 %6 %7 %8 %9" and be done with
it, but would like to know a way to provide more than 9 arguments to the
batchfile and still have it work.
Regards,
Rudy Wieser
Combine arguments separating by some known character and then split them
within the batch itself.
R.Wieser
2024-04-22 20:34:56 UTC
Permalink
Zaidy036,
Post by Zaidy036
Combine arguments separating by some known character and then split them
within the batch itself.
The batchfile receives any number of arguments just fine.

Its the part of providing them, minus %1, to an executable thats the
problem. And no, I can't rewrite those programs. Alas.

If I could I could just have them include a "ignore the second argument
(%1)" switch :-)

Regards,
Rudy Wieser
Loading...