Jake
Jake

Reputation: 33

What is the meaning of batch 'switches'

I have heard about all sorts of different batch commands like echo. but then there's different switches like /p or /I ect. what on the planet, is their difference? and what is the difference between !variable! and %variable%??? and %%i please tell me, who knows what advantages there could be. thanks

Upvotes: 0

Views: 728

Answers (2)

Magoo
Magoo

Reputation: 80033

There are commands like echo and dir and findstr.

For some commands, there are switches that modify how the command runs.

dir

will generate a directory-list

dir /od

will generate a directory-list in order of the dates that the files/directories were last modified.

A well-behaved command is self-documenting; that is

commandname /?

will show documentation.

This pribciple is largely supported by the built-in commands supplied as part of the OS and console-oriented commands provided by third parties.

BUT

Other operating systems use - or -- instead of / and when these commands are "ported" from those operating systems to Windows, they still require the alternative switch-indicators. It's a matter of knowing on a case-by-case basis.

The values of variables within a batch file are fundamentally retrieved using %var%. There are two exceptions - the value of a parameter supplied to the batch or subroutine is accessed by %n (where n=1-9) and %%x where x is the metavariable in a for loop (ie. the loop-control variable.) Bizarrely, %%x here is one of the few cases where batch is case-sensitive.

When batch executes a routine, it first parses the line (checks it for validity) and then executes it. A logical line may be many physical lines long. As part of the parsing process, any %var% is replaced by the value of that variable at the time the line is parsed.

Consequently, in a for loop, %var% is replaced by its value at parse-time so if it is changed by the loop, %var% will appear to have a constant value. To access the current value of var in the loop as it is changed, use !var! BUT in order to use the !var! syntax, you need to have previously executed setlocal enabledelayedexpansion.

Upvotes: 1

user6017774
user6017774

Reputation:

Type Help in a command prompt.

For each command type either help command (eg help dir) or command /? (eg dir /?)

For commands with sub modes like reg command type reg /? then reg query /?.

See Trouble with renaming folders and sub folders using Batch for !var!

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SetLocal EnableDelayedExpansion command

See setlocal /? and also for /?for an explanation on when and why to do it.

Upvotes: 0

Related Questions