Welcome Guest.   Make a donation to an author on the site January 31, 2015, 08:40:58 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Your Support Funds this Site: View the Supporter Yearbook.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Diffference between Switces and Parameters in Batch file?  (Read 385 times)
hulkbuster
Participant
*
Posts: 219


see users location on a map View Profile Give some DonationCredits to this forum member
« on: January 29, 2015, 11:50:44 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
Quote
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.
Logged

Autorun Virus Remover.V.2.3 / Look n Stop Firewall.v2.07 / Sandboxie.v4.08  / Some Sense
worstje
Honorary Member
**
Posts: 563



The Gent with the White Hat

View Profile Give some DonationCredits to this forum member
« Reply #1 on: January 29, 2015, 11:59:46 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.
Logged
Vurbal
Supporting Member
**
Posts: 544



Mostly harmless

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #2 on: January 29, 2015, 03:07:59 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.
Logged

Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read.
- Groucho Marx

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
**
Posts: 563



The Gent with the White Hat

View Profile Give some DonationCredits to this forum member
« Reply #3 on: January 29, 2015, 03:19:24 PM »

You are technically correct on pretty much everything, Vurbal. smiley

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.)
Logged
Vurbal
Supporting Member
**
Posts: 544



Mostly harmless

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #4 on: January 29, 2015, 04:28:44 PM »

That's understandable. Had I used the level of detail required to give anything resembling a "complete" answer, I'd still be typing.
Logged

Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read.
- Groucho Marx

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
***
Posts: 1,055


Member #520

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #5 on: January 30, 2015, 09:33:38 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.
?


Quote
I found one site that explains Parameters:Command line parameters
Quote
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

Quote
Hello hulkbuster


Call as: test1 "hulkbuster has a question"

Quote
Hello "hulkbuster has a question"


-

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


Call as: test2 "hulkbuster has a question"

Quote
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.

Quote
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:12 AM by AbteriX » Logged

Greetings, Stefan.
MilesAhead
Member
**
Posts: 5,250



View Profile WWW Give some DonationCredits to this forum member
« Reply #6 on: January 30, 2015, 09:58:16 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.

Logged

"I worked myself up from nothing to a state of extreme poverty."
- Groucho Marx
AbteriX
Charter Honorary Member
***
Posts: 1,055


Member #520

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #7 on: January 30, 2015, 03:15:26 PM »

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

Greetings, Stefan.
MilesAhead
Member
**
Posts: 5,250



View Profile WWW Give some DonationCredits to this forum member
« Reply #8 on: January 30, 2015, 03:36:19 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.  smiley
Logged

"I worked myself up from nothing to a state of extreme poverty."
- Groucho Marx
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.04s | Server load: 0.04 ]