ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Diffference between Switces and Parameters in Batch file?

<< < (2/2)

AbteriX:
Hi folks, i want to understand some differences between Switches and Parameters. -hulkbuster (January 29, 2015, 11:50 AM)
--- End quote ---


Switch = they ask the program or script to disable/enable something; on or off; yes/no. (e.g.: 'dir /AD' or 'dir /A-D')
Parameter = provide some information to the program or script. (e.g.: 'dir C:\Temp')
Arguments = the program or script takes an Switch or an Paramter as Argument to controll what it should do and how.


Me think,
- switch is a special kind of an parameter.
- parameter are what I call an application with to act as I want.
- arguments are what an application use to know what to do for me, provided by me as parameter.
?


I found one site that explains Parameters:Command line parameters
Batch files can only handle parameters %0 to %9

%0 is the program name as it was called,
%1 is the first command line parameter,
%2 is the second command line parameter,
and so on till %9.
--- End quote ---

What do i understand about %0, and %1 till %9. How does it can be applied in batch file.

Any suggestion from experts would be greatful.

--- End quote ---

Examples:

Batch file "Test1.cmd"
@ECHO OFF
ECHO Hello %1

Call as: test1 hulkbuster has a question

Hello hulkbuster
--- End quote ---


Call as: test1 "hulkbuster has a question"

Hello "hulkbuster has a question"
--- End quote ---


-

Batch file "Test2.cmd"
@ECHO OFF
ECHO Hello %~1

Call as: test2 "hulkbuster has a question"

Hello hulkbuster has a question
--- End quote ---


- - -

Batch file "ParameterPresenter2.cmd"
@ECHO OFF
ECHO.
ECHO 1:_%1_
ECHO 2:_%2_
ECHO 3:_%3_
ECHO 4:_%4_
ECHO 5:_%5_
ECHO 6:_%6_
ECHO 7:_%7_
ECHO 8:_%8_
ECHO 9:_%9_

Call as: ParameterPresenter "Hi folks," i want to "understand some" differences.

1:_"Hi folks,"_
2:_i_
3:_want_
4:_to_
5:_"understand some"_
6:_differences._
7:__
8:__
9:__
--- End quote ---

- - -


If I want to provide a Switch to my batch, like "Test3.cmd /?",
I would have to parse the arguments for '/?' and in case I found it,
switch in my code to the corresponding place:
Pseudo demo code
For ARG in AllArgs
   If ARG == "/?" Then GoTo _displayHelpMessage()
Next ARG


HTH?

MilesAhead:
Note that you can process additional arguments using Shift

As shown in the example, it works well in loops to process arguments until they run out.  Usually after the shift if %1 is blank the loop should exit as all the args have been processed.

AbteriX:
Thanks, but that would be a bit too complex for my explanation to hulkbuster.

MilesAhead:
Thanks, but that would be a bit too complex for my explanation to hulkbuster.
-AbteriX (January 30, 2015, 03:15 PM)
--- End quote ---

I added the note because he may have gotten the impression there was a hard limitation due to the remark in the original post.  Everything doesn't have to be conglomerated into one post.  :)

hulkbuster:
@Vurbal @AbteriX @worstje:  Thank you all for your reply, sorry i couldn't reply quick. Was out of town for some work.
Vurbal gave an extremely elegant explanation, i just got this post printed.
To refine my understanding of batch commands and parameter. I hope i will be able to do something with these explanation.
Thank you all for taking the trouble to explain in details.
No other forums does, what it does here.
I am glad to be here.

Thank you.

Navigation

[0] Message Index

[*] Previous page

Go to full version