topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Saturday December 14, 2024, 2:15 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Diffference between Switces and Parameters in Batch file?  (Read 8202 times)

hulkbuster

  • Participant
  • Joined in 2009
  • *
  • Posts: 259
    • View Profile
    • Donate to Member
Diffference between Switces and Parameters in Batch file?
« on: January 29, 2015, 11:50 AM »
Hi folks, i want to understand some differences between Switches and Parameters. I usually make batch files for compressing files into rar and zip
and opening different application to run. It works fine. But lately when i dig more about making batch file, i come across this term called Parameters.
I hardly do use it and i want to know from some experts what does it differentiates from Switches.
I google and try to get some definitive explanation but there are tonnes of it and i am not able to put one simple definition in my head.

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.

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.
ClipDiary 5.3/ Smadav 2018/ Some Sense

worstje

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 588
  • The Gent with the White Hat
    • View Profile
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #1 on: January 29, 2015, 11:59 AM »
Switches, parameters... They're basically different words for the same thing, although the context matters greatly.

Parameter usually refers to anything passed after the program / batch file itself.
Switches refer to things like -f, --preserve-root, /help, /? and so forth.

Suppose you have a batch file you call like: test.bat One Two Three Four Five Six

Then in that batchfile itself, you can refer to 'test.bat' with %0. If you want 'One', then you'd use %1, you'd use %2 for 'Two' and so forth.

Vurbal

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 653
  • Mostly harmless
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #2 on: January 29, 2015, 03:07 PM »
Although they're often used interchangeably, switch and parameter don't really mean the same thing. Or perhaps more accurately, the common usage of switch has rendered its definition nebulous at best, and meaningless at worst.

The tricky part, or at least the first one, is understanding the context of parsing arguments. All switches are arguments, but not all arguments are switches. Instead of focusing on batch files, it may be easier to get a handle on the concepts by looking at basic built in Windows commands. Let's say you type in the following:

dir /x /q

Windows (specifically cmd.exe) parses it, based on whitespace characters (including some special characters like commas and colons) to find parameters. Each group of consecutive whitespace characters is considered equal to a single space character for this purpose. The string of characters after the first space and before the second are assigned to %1. The string after the second space and before the third are assigned to %2. These are parameters. In this case we obviously have 2 parameters - /x and /q.

The dir command then parses each one individually to see what they mean. In this case, each parameter is interpreted as a switch that specifies what information and formatting to display the directory listing.

But you could type it like this instead:

dir /x/q

Using the same sequence as before, cmd.exe parses it, but since there are no whitespace characters between /x and /q, there is only 1 parameter. The dir command, on the other hand, recognizes that the single parameter contains the / character and separates it into 2 different switches.

Going back to my point about how the word switch is used, consider using the copy command:

copy file1.foo file2.bar

Based on how the command line is parsed, there are definitely 2 parameters. However, depending on who you ask there are either 0 or 2 switches. Personally I prefer the strict technical definition which says there are none. A switch does exactly what the name suggests. Essentially, a switch turns a predefined operation or feature on or off.

I believe, technically speaking, the batch file (or command) name is also considered a parameter (it's certainly treated like one) and it's not an argument at all. It certainly isn't a switch.
I learned to say the pledge of allegiance
Before they beat me bloody down at the station
They haven't got a word out of me since
I got a billion years probation
- The MC5

Follow the path of the unsafe, independent thinker. Expose your ideas to the danger of controversy. Speak your mind and fear less the label of ''crackpot'' than the stigma of conformity.
- Thomas J. Watson, Sr

It's not rocket surgery.
- Me


I recommend reading through my Bio before responding to any of my posts. It could save both of us a lot of time and frustration.

worstje

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 588
  • The Gent with the White Hat
    • View Profile
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #3 on: January 29, 2015, 03:19 PM »
You are technically correct on pretty much everything, Vurbal. :)

I just wanted to keep it simple and to the point for the sake of clarity. (That and I had to dash out for a meeting, so I didn't have the time to be very in-depth.)

Vurbal

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 653
  • Mostly harmless
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #4 on: January 29, 2015, 04:28 PM »
That's understandable. Had I used the level of detail required to give anything resembling a "complete" answer, I'd still be typing.
I learned to say the pledge of allegiance
Before they beat me bloody down at the station
They haven't got a word out of me since
I got a billion years probation
- The MC5

Follow the path of the unsafe, independent thinker. Expose your ideas to the danger of controversy. Speak your mind and fear less the label of ''crackpot'' than the stigma of conformity.
- Thomas J. Watson, Sr

It's not rocket surgery.
- Me


I recommend reading through my Bio before responding to any of my posts. It could save both of us a lot of time and frustration.

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,149
    • View Profile
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #5 on: January 30, 2015, 09:33 AM »
Hi folks, i want to understand some differences between Switches and Parameters.


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.

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.

Examples:

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


Call as: test1 hulkbuster has a question

Hello hulkbuster


Call as: test1 "hulkbuster has a question"

Hello "hulkbuster has a question"


-

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


Call as: test2 "hulkbuster has a question"

Hello hulkbuster has a question


- - -

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:__

- - -


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?
« Last Edit: January 30, 2015, 09:39 AM by AbteriX »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #6 on: January 30, 2015, 09:58 AM »
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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,149
    • View Profile
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #7 on: January 30, 2015, 03:15 PM »
Thanks, but that would be a bit too complex for my explanation to hulkbuster.

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #8 on: January 30, 2015, 03:36 PM »
Thanks, but that would be a bit too complex for my explanation to hulkbuster.

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

  • Participant
  • Joined in 2009
  • *
  • Posts: 259
    • View Profile
    • Donate to Member
Re: Diffference between Switces and Parameters in Batch file?
« Reply #9 on: March 20, 2015, 02:14 PM »
@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.
ClipDiary 5.3/ Smadav 2018/ Some Sense