Al
Sun Feb 20 19:09:07 CST 2005
"Roland Hall" <nobody@nowhere> wrote in message
news:uBg8EM5FFHA.1296@TK2MSFTNGP10.phx.gbl...
> "Rick" <None@none.com> wrote in message
> news:u5hh11d6q5daodebc2esfnt3ohc8618fup@4ax.com...
> :I have another question maybe you could help me with, the following
> : line
> :
> : @if not "%OS%"=="Windows_NT" (goto :LOGOFF)
>
> In a batch file, the @ symbol at the beginning of a line will hide the
line
> from being displayed. It is equivalent or == to Echo off. Perhaps you've
> seen @Echo Off. The @ hides the first line and then Echo Off hides the
rest
> of them.
>
> The ==, as stated above is the equivalent so the user is looing for an
> environment variable, which you can see at a command prompt when you type
> in: set. This will show you all environment variables. However, you can
> also limit it to one easily if you type in: echo %OS%
> Doing this on my XP system I get:
> c:\>echo %OS%
> Windows_NT
As an aside, I do not think this works for 9x.
> I've never seen the () around a goto statement and : is not needed
although
> that is the way you make a label.
Parentheses are used to group a set of commands to create a compound
command. The set of commands must number AT LEAST ONE. I don't use it for
single goto's like that, but I sometimes use it like this:
(set var=no trailing blanks here!)
To ensure that I do not inadvertently have a trailing blank included in the
variable.
> Ex.
>
> @echo off
> if not "%OS%"=="Windows_NT" goto LOGOFF
Also, I prefer to explicitly keep the ":" in the name of the label, i.e.:
goto:LOGOFF
This serves to make all references to labels syntactically consistent, i.e.:
call:mySub arg1 arg2
> .
> .
> .
> :LOGOFF
> echo Thanks for visiting Wally World.
> echo See you next year!
>
> :
> : is this say if the OS is NOT Windows NT(XP) go to logoff or IS Windows
> : XP?
>
> Windows NT, 2K and XP all show Windows_NT because they are based on it.
UNLESS the SET command is used to modify its value.
> The other thing you may not have noticed was the "" around %OS% and
> Windows_NT.
> Double quotes are generally put around paths, variable names when spaces
are
> present so the variable or path is not parsed and is taken as whole.
> However, it was not used like that here. If this batch file was run on an
> OS not based on Windows NT, then it would not be equivalent to Windows_NT,
> which means it would return a blank value. You cannot test a blank value
so
> the double quotes provide a value > blank.
>
> Ex. If the OS was something else it would return "" and the test would
> fail.
Erm, it wouldn't fail, it would just detect an inequality. That would be a
success, as the purpose would be to avoid running code that is not designed
for the o/s in effect.
And, while non-NT o/s's that run batch files generally do not set the OS
variable to anything, this does not prevent one's script from setting it to,
for example, "Windows98". We had a huge problem at one site where the
standard 98 client was modified to set this value in the autoexec.bat file
(the guy who did this left without documenting it). A later change to the
logon script, where they tested for "", came to the wrong conclusion as to
the O/S and it took a week to find the cause.
/Al
> "" == "Windows_NT"? fails and would then make the statement true and
branch
> to LOGOFF. If it were based on Windows NT, it would return false because
> "Windows_NT" == "Windows_NT".
>
> If the quotes were missing and this was run on say, Windows 98, I don't
> think there is an OS environment variable so it would return blank and
that
> would case the batch process to error.
>
> I do mine a little different but with the same concept in mind.
>
> @echo off
> if not /%OS% == /Windows_NT goto LOGOFF
> .
> .
> .
> :LOGOFF
>
> Hope that clears it up.
>
> --
> Roland Hall
> /* This information is distributed in the hope that it will be useful, but
> without any warranty; without even the implied warranty of merchantability
> or fitness for a particular purpose. */
> Technet Script Center -
http://www.microsoft.com/technet/scriptcenter/
> WSH 5.6 Documentation -
http://msdn.microsoft.com/downloads/list/webdev.asp
> MSDN Library -
http://msdn.microsoft.com/library/default.asp
>
>