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

Parameters

8 views
Skip to first unread message

frank

unread,
May 6, 2004, 9:07:23 AM5/6/04
to
I know that %* will give you all of the parameters passed to the batch, but
how can you get from the nth-parameter on? I want to pass everything from
the 2nd parameter on to another batch file. I am currently passing it %2 %3
%4 %5 ... %9 (not that I anticipate ever having more than nine parameters,
but who knows?)


foxidrive

unread,
May 6, 2004, 9:43:18 AM5/6/04
to

Try this


@echo off
set var=%*
call set var=%%var:*%1=%%
echo %var%

Marco Maier Said

unread,
May 6, 2004, 10:20:42 AM5/6/04
to
foxidrive wrote in message <news:9s0xk6vf6dj3$.1oq9b0ci...@40tude.net>
:

> @echo off
> set var=%*
> call set var=%%var:*%1=%%
> echo %var%

It couldn't work properly if two or more parameters are equal

--
Marco

foxidrive

unread,
May 6, 2004, 10:26:01 AM5/6/04
to

You really should test first, Marco. :)

===[capture]===
D:\>a.bat 1 2 3 4 1
2 3 4 1
===[capture]===

Matthias Tacke

unread,
May 6, 2004, 10:35:36 AM5/6/04
to
foxidrive wrote:

Hello Foxidrive,

took me a bit longer for a solution. And then I had to test yours.
I see a problem when an argument is conatined in another one. Of course
this example is a construed one to output from the 4th.

==Screen copy==========================================================
C:\Test>ParamTest.cmd "first argument" 2 e 4 5 6 7 8 9 10 11 12
Var=[nt" 2 e 4 5 6 7 8 9 10 11 12]
Drücken Sie eine beliebige Taste . . .
Px1="first argument"
Px10=10
Px11=11
Px12=12
Px2=2
Px3=e
Px4=4
Px5=5
Px6=6
Px7=7
Px8=8
Px9=9
NewPar=[ 4 5 6 7 8 9 10 11 12]

C:\Test>
==Screen copy==========================================================

::ParamTest.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@Echo off & setlocal

:: from 4th parameter - Foxidrive
set var=%*
call set var=%%var:*%3=%%
echo Var=[%var%]
Pause

:: ditto my variant
:: Store Parameters
set ParCnt=0
call :Par2Var %*
:: Show Parameters
set Px
:: Build new paramters from 4nd
if defined NewPar set NewPar=
for /L %%A in (4,1,%ParCnt%) do call :Var2PAr %%A
:: Show New Set
echo NewPar=[%NewPar%]
goto :eof
:Par2Var
if "%~1" EQU "" goto :eof
set /A ParCnt+=1
Call set "Px%ParCnt%=%1"
shift
goto :Par2Var
:Var2Par
Call set NewPar=%NewPar% %%Px%1%%
::ParamTest.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm

John Zeman

unread,
May 6, 2004, 10:38:38 AM5/6/04
to
On Thu, 06 May 2004 13:07:23 GMT, frank wrote:

Here is a snippet from a script I wrote to build a Winamp playlist from the
songs I have selected in my file manager. There are usually far more than
9 songs selected, this reads them in one at a time and builds a playlist
file of them.

John


:loop

:: WITHIN THIS LOOP IF %1 IS A NULL THEN IT'S TIME TO BOOGIE
if {%1}=={} goto:play

:: OTHERWISE BUILD THE PLAYLIST FILE
echo %1>>c:\temp\playlist.m3u

:: SHIFT %1 TO THE NEXT SELECTED SONG
shift /1
goto:loop


:play
<Run Winamp and play selected >songs

frank

unread,
May 6, 2004, 10:42:55 AM5/6/04
to
"foxidrive" wrote in message
news:9s0xk6vf6dj3$.1oq9b0cibzg72.dlg@40tude.net...

> @echo off
> set var=%*
> call set var=%%var:*%1=%%
> echo %var%

I made a slight modification to remove the trailing space from the first
parameter showing up as a leading space in the new parameter list:
call set var=%%var:*%1 =%%
and it seemed to work fine.
Why do you have to "call" the 2nd set command for it to work?


Marco Maier Said

unread,
May 6, 2004, 11:03:40 AM5/6/04
to
frank wrote in message
<news:fMqmc.17025$urx....@news04.bloor.is.net.cable.rogers.com> :

@echo off
for /f "tokens=1*" %%a in ('echo %*') do echo %%b

--
Marco

Marco Maier Said

unread,
May 6, 2004, 11:13:47 AM5/6/04
to
foxidrive wrote in message
<news:1r1yakmdym7me.1383laseupni1$.d...@40tude.net> :

> You really should test first, Marco. :)
>
> ===[capture]===
> D:\>a.bat 1 2 3 4 1
> 2 3 4 1
> ===[capture]===

I was referring to a more general case

@echo off
set var=%*
call set var=%%var:*%10=%%
echo %var%

D:\>a.bat 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1

--
Marco

foxidrive

unread,
May 6, 2004, 11:16:58 AM5/6/04
to
On Thu, 6 May 2004 16:35:36 +0200, Matthias Tacke wrote:

>>On Thu, 06 May 2004 13:07:23 GMT, frank wrote:
>>
>>> I know that %* will give you all of the parameters passed to the batch, but
>>> how can you get from the nth-parameter on? I want to pass everything from
>>> the 2nd parameter on to another batch file. I am currently passing it %2 %3
>>> %4 %5 ... %9 (not that I anticipate ever having more than nine parameters,
>>> but who knows?)
>>

> took me a bit longer for a solution. And then I had to test yours.
> I see a problem when an argument is conatined in another one. Of course
> this example is a construed one to output from the 4th.

Yes, and that may have been what Marco meant too, but for the stated task
it works fine AFAICS.

To Frank - the call and added percent signs in the set command forces the
variable %1 to be evaluated, whereas without the call it can't be used with
that syntax.

foxidrive

unread,
May 6, 2004, 11:26:42 AM5/6/04
to

Sorry to say Marco, but it fails the simplest test.

===[capture]===
D:\>a.bat "a b" c
b" c
===[capture]===

I personally am guilty of concentrating on the aim of the question, and
disgarded the more general "nth-parameter on" portion, so my solution is
only good for case above.

Matthias Tacke

unread,
May 6, 2004, 11:31:37 AM5/6/04
to
Marco Maier Said wrote:

Hi Marco,
I see a problem with quoted arguments here.

Thats why my version uses a call %* to resemble the same parsing
algorithm. Of course >|< & will be difficult to escape/quote properly.

Marco Maier Said

unread,
May 6, 2004, 1:18:28 PM5/6/04
to
Matthias Tacke wrote in message <news:c7dloo$7gr$04$1...@news.t-online.com> :

> Hi Marco,
> I see a problem with quoted arguments here.

What about this?

@echo off
setlocal enabledelayedexpansion
set a=%%3
for /l %%b in (4 1 9) do set a=!a! %%%%b%
call echo.%a%

--
Marco

Matthias Tacke

unread,
May 6, 2004, 1:37:06 PM5/6/04
to
Marco Maier Said wrote:

Nice. More direct. But limited to the remaining 6 of 9 arguments.
When you shift and then take the remaining it still suffers from being
stuck to the max 9 reachable parameters via %1..%9.
And the OP's question was how to overcome that limitation.

Marco Maier Said

unread,
May 6, 2004, 1:49:55 PM5/6/04
to
Matthias Tacke wrote in message <news:c7dt41$eli$03$1...@news.t-online.com> :

> Nice. More direct. But limited to the remaining 6 of 9 arguments.
> When you shift and then take the remaining it still suffers from being
> stuck to the max 9 reachable parameters via %1..%9.
> And the OP's question was how to overcome that limitation.

Ah,yes,and this one?


@echo off
set a=%*
call set a=%%a:*%1 %2 %3 %4=%%
echo.%a%

--
Marco

Matthias Tacke

unread,
May 6, 2004, 2:11:32 PM5/6/04
to
Marco Maier Said wrote:

Provided ther is no equal sign in any of the arguments 1-4 it will do
better.

==screen=copy==========================================================
C:\Test>ParamTest.cmd "first argument" "a=b" e 4 5 6 7 8 9 10 11 12
Foxidrive Var=nt" "a=b" e 4 5 6 7 8 9 10 11 12
=================================
Marco Maier Said=e 4 5 6 7 8 9
=================================
Marco Maier Said2=b" e 4==b" e 4 5 6 7 8 9 10 11 12
=================================


Px1="first argument"
Px10=10
Px11=11
Px12=12

Px2="a=b"


Px3=e
Px4=4
Px5=5
Px6=6
Px7=7
Px8=8
Px9=9
NewPar=[ 4 5 6 7 8 9 10 11 12]

C:\Test>type ParamTest.cmd
@Echo off & setlocal enabledelayedexpansion


:: from 4th parameter - Foxidrive

set var=%*
call set var=%%var:*%3=%%
echo Foxidrive Var=%var%
echo =================================
:: Marco's way


set a=%%3
for /l %%b in (4 1 9) do set a=!a! %%%%b%

call echo.Marco Maier Said=%a%
echo =================================
:: Marco2


set a=%*
call set a=%%a:*%1 %2 %3 %4=%%

echo.Marco Maier Said2=%a%
echo =================================


:: ditto my variant
:: Store Parameters
set ParCnt=0
call :Par2Var %*
:: Show Parameters
set Px
:: Build new paramters from 4nd
if defined NewPar set NewPar=
for /L %%A in (4,1,%ParCnt%) do call :Var2PAr %%A
:: Show New Set
echo NewPar=[%NewPar%]
goto :eof
:Par2Var
if "%~1" EQU "" goto :eof
set /A ParCnt+=1
Call set "Px%ParCnt%=%1"
shift
goto :Par2Var
:Var2Par
Call set NewPar=%NewPar% %%Px%1%%

C:\Test>
==screen=copy==========================================================

Marco Maier Said

unread,
May 6, 2004, 2:46:16 PM5/6/04
to
Matthias Tacke wrote in message <news:c7dv4k$g51$00$1...@news.t-online.com> :

> Provided ther is no equal sign in any of the arguments 1-4 it will do
> better.

Yes it doesn't work only with =, while yours doesn't work with <|>
%path% etc....

--
Marco

Timo Salmi

unread,
May 8, 2004, 1:16:59 PM5/8/04
to
40) How do I get the number of arguments given to a script?

243153 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi

This task is not quite as trivial as it first appears to be. There
are a couple subtle pitfalls. Since the solution uses shift it
"destroys" the parameters from later usage, so they have to be
stored. The logic is that if one wants to count the parameters, one
is likely also to want to use the parameters! Also note the usage of
[] in the if testing lest there is confusion with parameters
potentially enclosed in quotes. If one needs a parameter count, it
is logical and sensible to always put it as the first thing into the
script. The denotations in the example below have clear UNIX
connotations.

@echo off & setlocal enableextensions enabledelayedexpansion
set /a argc=0
:_argcLoop
if [%1]==[] goto _exitargcLoop
set /a argc+=1
set arg%argc%=%1
shift
goto _argcLoop
:_exitargcLoop
::
:: Show the results
echo The number of parameters is %argc%
set /a count_=0
:_dispLoop
set /a count_+=1
if defined arg%count_% (echo !arg%count_%! & goto _dispLoop)
::
endlocal & goto :EOF

An example:
D:\TEST>cmdfaq one two "the third"
The number of parameters is 3
one
two
"the third"

If you wish to omit the quotes (") substitute
set arg%argc%=%~1

References/Comments:
http://www.google.com/groups?selm=bs6li2$bth$04$1...@news.t-online.com

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Marco Maier Said

unread,
May 8, 2004, 3:38:22 PM5/8/04
to
Matthias Tacke wrote in message <news:c7dt41$eli$03$1...@news.t-online.com> :

> Nice. More direct. But limited to the remaining 6 of 9 arguments.
> When you shift and then take the remaining it still suffers from being
> stuck to the max 9 reachable parameters via %1..%9.
> And the OP's question was how to overcome that limitation.

Here is the working version!

@echo off
for /l %%_ in (1 1 4) do shift
set _=%1
:_
shift
if %1_==_ goto :__
set _=%_% %1
goto :_
:__
echo %_%

--
Marco

Al Dunbar

unread,
May 9, 2004, 3:18:57 PM5/9/04
to

"Timo Salmi" <t...@UWasa.Fi> wrote in message
news:c7j4mb$3...@poiju.uwasa.fi...

> 40) How do I get the number of arguments given to a script?
>
> 243153 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
> tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi
>
> This task is not quite as trivial as it first appears to be.

No kidding...

> There
> are a couple subtle pitfalls. Since the solution uses shift it
> "destroys" the parameters from later usage, so they have to be
> stored. The logic is that if one wants to count the parameters, one
> is likely also to want to use the parameters!

OK, so below is my solution. It does not use shifting, but it is an external
batch utility. I expect it could be encoded as an internal routine, i.e.
"call:getarg varname 3 5 %*".

It creates a variable (whose name is its first parameter) to contain all
parameters from {begin} to {end}, suitable for passing on to another batch
file. If the calling routine needs to extract individual parameters
separately, this could be done by setting the end parameter to the same
value as the bgn parameter.

/Al

C:\> type getarg.cmd
::: usage: call arg.cmd varname begin end %*
@echo off
setlocal

set _var=%1
set/a _bgn = %2 + 3
set/a _end = %3 + 3
(set _args=)
for /l %%F in (%_bgn%,1,%_end%) do call set _args=%%_args%% %%%%F
endlocal & set %_var%=%_args:~1%

C:\> type getargtest.cmd
@echo off

call getarg test 1 1 %*
echo/[%test%]
call getarg test 1 2 %*
echo/[%test%]
call getarg test 1 3 %*
echo/[%test%]
call getarg test 2 2 %*
echo/[%test%]
call getarg test 2 3 %*
echo/[%test%]
call getarg test 3 3 %*
echo/[%test%]

C:\> getargtest "arg 1" "arg 2" "ar
g 3"
["arg 1"]
["arg 1" "arg 2"]
["arg 1" "arg 2" "arg 3"]
["arg 2"]
["arg 2" "arg 3"]
["arg 3"]

C:\> getargtest AA BB
[AA]
[AA BB]
[AA BB ]
[BB]
[BB ]
[]

C:\>

Dr John Stockton

unread,
May 9, 2004, 10:41:56 AM5/9/04
to
JRS: In article <c7j4mb$3...@poiju.uwasa.fi>, seen in
news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Sat, 8 May
2004 20:16:59 :

> 40) How do I get the number of arguments given to a script?
>
> 243153 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
> tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi


IF, as IIRC it *may*, %* will pass all the arguments, then ISTM that a
batch file could call another batch file to count them with shift; and,
moreover, could call WSH using javascript or vbscript to determine the
number very easily (for the WSH part, see my batfiles.htm#WSH for
detail.

I assume that standard output can be captured to the environment.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Timo Salmi

unread,
May 10, 2004, 3:31:33 AM5/10/04
to
Al Dunbar <alan-no-...@hotmail.com> wrote:

> "Timo Salmi" <t...@UWasa.Fi> wrote:
> > 40) How do I get the number of arguments given to a script?

> OK, so below is my solution. It does not use shifting, but it is an external

Al, John, thanks. The Google references to your postings included
into the FAQ.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

0 new messages