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?

(1/2) > >>

hulkbuster:
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.
--- 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.

worstje:
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:
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.

worstje:
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:
That's understandable. Had I used the level of detail required to give anything resembling a "complete" answer, I'd still be typing.

Navigation

[0] Message Index

[#] Next page

Go to full version