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

Evaluating command line switches

16 views
Skip to first unread message

Rob

unread,
Apr 12, 2006, 1:46:47 PM4/12/06
to
Rather than reinvent the wheel, does anyone have a snippet of code they
could share on how to evaluate command line switches in batch?
For example c:\>program.exe /a /b /c etc.
With regular batch parameter passing, if you omit the second param, the
third evaluates as the second.
I need to be able to pass any combination of a, b and c to my routine and
have them evaluated correctly..
Thanks -Rob-


Phil Robyn

unread,
Apr 12, 2006, 2:02:12 PM4/12/06
to
Rob wrote:

- - - - - - - begin screen capture WinXP MCE 2005 SP2 - - - - - - -
c:\cmd>demo\DetectParms program.exe /a /c
slasha=yes
slashc=yes

c:\cmd>demo\DetectParms program.exe /b /c
slashb=yes
slashc=yes

c:\cmd>demo\DetectParms program.exe /c /b /a
slasha=yes
slashb=yes
slashc=yes

c:\cmd>demo\DetectParms program.exe /z
Environment variable slash not defined

c:\cmd>wyllist demo\DetectParms.cmd
==========begin file c:\CMD\DEMO\DetectParms.cmd ==========
01. @echo off
02. setlocal enabledelayedexpansion
03. for %%a in (a b c) do (
04. echo/%*|findstr /c:"/%%a">nul
05. if !errorlevel! equ 0 set slash%%a=yes
06. )
07. set slash|find "yes"
==========end file c:\CMD\DEMO\DetectParms.cmd ==========
- - - - - - - end screen capture WinXP MCE 2005 SP2 - - - - - - -


--
Phil Robyn
University of California, Berkeley

Rob

unread,
Apr 13, 2006, 11:51:44 AM4/13/06
to

"Phil Robyn" <phil_...@comcast.net> wrote in message
news:fdGdncsTYoU43aDZ...@comcast.com...

Thanks Phil. That helps a bit and gives me a couple ideas, but I probably
didn't describe the problem clearly enough. /a /b /c were meant to
represent variables passed to the program. eg /a=1234 /b=abcd /c=1a2b etc.
The program must put any combination of these variables into the appropriate
environment variable for further processing.
Thanks -Rob-


foxidrive

unread,
Apr 13, 2006, 12:06:43 PM4/13/06
to

You cannot use /a=1234 /b=abcd /c=1a2b etc. unless they are each double
quoted, is that what you mean?

The equals sign is a delimiter on the command line.

Timo Salmi

unread,
Apr 13, 2006, 1:10:00 PM4/13/06
to
Rob wrote:
> I probably didn't describe the problem clearly enough. /a /b /c
> were meant to represent variables passed to the program. eg
> /a=1234 /b=abcd /c=1a2b etc. The program must put any combination
> of these variables into the appropriate environment variable for
> further processing.

@echo off & setlocal enableextensions enabledelayedexpansion
set p1=%1
set p2=%2
set p3=%3
for %%p in (p1 p2 p3) do (
if defined %%p echo !%%p!|find /i "/a">nul
if !errorlevel! EQU 0 (
set a_=!%%p!
set a_=!a_:/a=!
)
if defined %%p echo !%%p!|find /i "/b">nul
if !errorlevel! EQU 0 (
set b_=!%%p!
set b_=!b_:/b=!
)
if defined %%p echo !%%p!|find /i "/c">nul
if !errorlevel! EQU 0 (
set c_=!%%p!
set c_=!c_:/c=!
)
)
echo a_=%a_%
echo b_=%b_%
echo c_=%c_%
endlocal & goto :EOF

Output:
C:\_D\TEST>cmdfaq /babcd /c1a2b /a1234
a_=1234
b_=abcd
c_=1a2b

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

Phil Robyn

unread,
Apr 13, 2006, 5:48:28 PM4/13/06
to

Well, yeah, I think you haven't fully described the problem. So what are the
appropriate environment variables?

Rob

unread,
Apr 18, 2006, 4:06:30 PM4/18/06
to

"Phil Robyn" <phil_...@comcast.net> wrote in message
news:xd-dnW2WpYm...@comcast.com...

>>
>
> Well, yeah, I think you haven't fully described the problem. So what are
> the
> appropriate environment variables?
>
> --
> Phil Robyn
> University of California, Berkeley

OK, the first one is an IP address, the second is a name string and the
third is a pocketsize.
Would something like this work...
my /a:pad /be:name /co:size
Timor had an interesting solution in a previous reply that I will also have
to explore.
Thanks -Rob-


Rob

unread,
Apr 19, 2006, 1:43:59 PM4/19/06
to
Hi Timo.
I think I follow most of your example but there is something I don't
understand. How does the set a_=!a_:/a=! work?
Also if I omit the /b parameter as in cmdfaq /a1234 /c1a2b it doesn't run
correctly. I get
a_=/a=
b_=/b=
c_=1a2b
I haven't been able to figure out why yet.
Thanks -Rob-


"Timo Salmi" <t...@uwasa.fi> wrote in message
news:443e85ec$0$7489$9b53...@news.fv.fi...

Timo Salmi

unread,
Apr 19, 2006, 4:53:37 PM4/19/06
to
Rob wrote:
> Hi Timo. I think I follow most of your example but there is
> something I don't understand. How does the set a_=!a_:/a=! work?

Write SET /? and read the explanation after

Environment variable substitution has been enhanced as follows:
%PATH:str1=str2%

> Also if I omit the /b parameter as in cmdfaq /a1234 /c1a2b it
> doesn't run correctly.

Quite, it doesn't. It requires three parameters. But the task is
artificial anyway. Unnecessary complications. Usually one would
simply use straight parameters is in

%1 %2 %3

without the /b parameter complications. The only advantage is that
the parameters could be given in any order. Why make a script that
complicated?

0 new messages