Re: Subst question

From: Al Dunbar [MS-MVP] (alan-no-drub-spam_at_hotmail.com)
Date: 02/21/05


Date: Sun, 20 Feb 2005 18:09:07 -0700


"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
>
>



Relevant Pages

  • Re: writing to a - log file
    ... MS-DOS (or command line session under Windows) you're using. ... But back to you redirection question. ... ECHO This is a test ...
    (comp.os.msdos.programmer)
  • Re: Persisting env vars in cmd windows
    ... The help text for the command you question goes like this: ... format by typing this: echo %date% ... If you don't then the batch file will fail. ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: 16-bit under WinXP Pro?
    ... @echo off ... If Command Extensions are enabled, and running on the Windows XP ... [[To run 16-bit Windows applications, NT uses a VDM that contains an extra ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Need Help with a DOS script (if possible)
    ... were meant to run the batch file with the "echo" command in place in the ... It never did but you removed the "echo" command regardless. ... echo Checking the target folder xxx%Target%yyy ...
    (microsoft.public.windowsxp.general)
  • Re: How To Execute Batch File Line By Line
    ... COMMAND /C string Carries out the command specified by string, ... CMD /C Carries out the command specified by string and then terminates. ... Both .cmd and .bat files work on Windows NT versions. ... File Description MS-DOS Batch File ...
    (microsoft.public.windowsxp.general)