views:

154

answers:

2
if "%OS%"=="Windows_NT" @setlocal
...
if "%OS%"=="Windows_NT" @endlocal

Does the above basically mean this:

if(OS == 'Windows_NT'):
...
endif

I've curious what's setlocal for ?

EDIT

Now the only uncertain is:how do bat identify the endif?

+2  A: 

http://ss64.com/nt/setlocal.html

Roberto Aloi
+3  A: 

try

setlocal /? 

on command prompt

As for if statements: help if should show you everything you need to know. Batch files have only single line if statements of the forms

if    [condition]    [statement]
if    [condition]    [statement]    else    [statement]

However, [statement] can be a block, delimited by parentheses:

if    [condition]    (
    [statement]
    [statement]
    ...
)

if    [condition]    (
    [statement]
    ...
)    else    (
    [statement]
    ...
)

There is no explicit end if keyword. The end of the if statement is marked by the end of the line or by the end of the parenthesized block.

Also keep in mind that you need to be careful with setting and subsequently using environment variables in a single block. Read up in help set on delayed expansion for the pitfalls there.

S.Mark
Then how does bat indentify the end of `if`?
@user: as far as I remember .bat files only support single-line if. So the end of the line is the end of the if-statement (maybe it was even single-statement).
Joachim Sauer
I took the liberty of expanding this on the `end if` issue, trivial as it may be :-)
Joey
Thanks for completing my answer, I've made it as wiki, thanks Johannes Rössel
S.Mark
I just tried,`else` seems not supported when the block takes up several lines:if 1 == 1 ( echo 1 echo good ) else ( echo 2 echo bad ),but works when it's single line