Bonehead batch file question

C

Craig

Hi folks,
I have a simple batch file that I've written that doesn't
properly detect which OS I'm running, and I'm not sure
why. Here it is:

@echo off
echo.
for /F "tokens=2* delims= " %%A IN ('REG
QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion" /v ProductName') do set os=%%B

echo %os%
pause

If "%OS%" == Microsoft Windows XP
goto xp

If "%OS%" == Microsoft Windows 2000
goto w2k

goto exit

:w2k
echo You're running Windows 2000
pause
exit

:xp
echo You're running Windows XP
pause

:exit
exit

What am I doing wrong??? Also, if this batch file is run
on another OS (Win98, etc.), it should just exit, right???

Thank you,
Craig
 
B

Bill Stewart

Craig said:
I have a simple batch file that I've written that doesn't properly detect
which OS I'm running, and I'm not sure why. Here it is:

@echo off
echo.
for /F "tokens=2* delims= " %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\
SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName') do set
os=%%B

echo %os%
pause

If "%OS%" == Microsoft Windows XP
goto xp

If "%OS%" == Microsoft Windows 2000
goto w2k

goto exit

:w2k
echo You're running Windows 2000
pause
exit

:xp
echo You're running Windows XP
pause

:exit
exit

What am I doing wrong??? Also, if this batch file is run on another OS
(Win98, etc.), it should just exit, right???

Hi Craig,

Isn't this the same thing you asked a couple of weeks ago?

In any case, the answer to your question is "no." It won't "just exit." It
will fail with a syntax error on Win9x because Win9x command.com's FOR
command does not have a /F switch.

A better alternative to all of this is to use my OSVER.EXE, which works on
Win9x. On NT and later, it can also tell you which service pack, and
whether the machine is a server, DC, or workstation.

http://home.comcast.net/~stewartb/wast.html

Bill
 
C

Craig

Hi Bill,
Yes, it's the same batch file, and although the "for" loop
works at grabbing the registry keys' value, the file
itself didn't quite work the way I want (it seems to
always default to the :xp section). But, I'll check out
your suggestion. Thanks.

Craig
 
M

Matthias Tacke

Craig said:
Hi folks,
I have a simple batch file that I've written that doesn't
properly detect which OS I'm running, and I'm not sure
why. Here it is:

I had a quick look into your previous postings. Seems you once knew
better, or you don't understand at all.
@echo off
echo.
for /F "tokens=2* delims= " %%A IN ('REG
QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion" /v ProductName') do set os=%%B

Why did you choose the variable os which already exists in uppercase?
echo %os%
pause

If "%OS%" == Microsoft Windows XP
goto xp

The above two lines have to be one. The comparison can't be succesful
if you put only one part in parenthes. That's per se a difference.
If "%OS%" == Microsoft Windows 2000
goto w2k

goto exit

:w2k
echo You're running Windows 2000
pause
exit

:xp
echo You're running Windows XP
pause

:exit
exit

What am I doing wrong??? Also, if this batch file is run
on another OS (Win98, etc.), it should just exit, right???

Thank you,
Craig

Take small pieces of code and test individually before putting all
together. And try to omit new errors when eliminating old ones.

And rtfm aka help command or command /? especially
if /?

hth
Matthias
 
O

Ole

If "%OS%" == "XP" goto xp
If "%OS%" == "2000" goto 2000
If "%OS%" == "NT" goto NT
if "%OS%" == "" goto notnt
:: if you get no data from %os% then you know its a non-nt OS
:: then use these: -

:notnt
:: > nul suppresses output from the ver command
ver | find "98" > nul
if errorlevel 0 goto 98
ver | find "95" > nul
if errorlevel 0 goto 95
ver | find "ME" > nul
if errorlevel 0 goto ME

you can simply this if you only want it to run on NT and just skip all the
non-nt detection and abort.

--
Ole

Craig said:
Hi folks,
I have a simple batch file that I've written that doesn't
properly detect which OS I'm running, and I'm not sure
why. Here it is:

I had a quick look into your previous postings. Seems you once knew
better, or you don't understand at all.
@echo off
echo.
for /F "tokens=2* delims= " %%A IN ('REG
QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion" /v ProductName') do set os=%%B

Why did you choose the variable os which already exists in uppercase?
echo %os%
pause

If "%OS%" == Microsoft Windows XP
goto xp

The above two lines have to be one. The comparison can't be succesful
if you put only one part in parenthes. That's per se a difference.
If "%OS%" == Microsoft Windows 2000
goto w2k

goto exit

:w2k
echo You're running Windows 2000
pause
exit

:xp
echo You're running Windows XP
pause

:exit
exit

What am I doing wrong??? Also, if this batch file is run
on another OS (Win98, etc.), it should just exit, right???

Thank you,
Craig

Take small pieces of code and test individually before putting all
together. And try to omit new errors when eliminating old ones.

And rtfm aka help command or command /? especially
if /?

hth
Matthias
 
G

Guest

Insert your commands under the lables :)2000 :XP, etc.)
Works in all versions of Windows.

--------Start of File--------

@ECHO OFF
:VER95
VER | FIND/I "WINDOWS 95"
IF ERRORLEVEL 1 GOTO VER98
GOTO 95
:VER98
VER | FIND/I "WINDOWS 98"
IF ERRORLEVEL 1 GOTO VERME
GOTO 98
:VERME
VER | FIND/I "WINDOWS ME"
IF ERRORLEVEL 1 GOTO VERNT
GOTO ME
:VERNT
VER | FIND/I "WINDOWS NT"
IF ERRORLEVEL 1 GOTO VER2000
GOTO NT
:VER2000
VER | FIND/I "WINDOWS 2000"
IF ERRORLEVEL 1 GOTO VERXP
GOTO 2000
:VERXP
VER | FIND/I "WINDOWS XP"
IF ERRORLEVEL 1 GOTO END
GOTO XP
:95

GOTO END
:98

GOTO END
:ME

GOTO END
:NT

GOTO END
:2000

GOTO END
:XP

GOTO END
:END

--------End of File--------

Austin M. Horst
 
T

Thanatos

Craig,

First of all, if you put quotes around the env variable in the IF statement,
then you must also put quotes around the comparison string on the right of
the == signs.
Otherwise the comparison will never be true. e.g.; IF "%OS%"==Windows_NT
...... will evaluate to: IF "Windows_NT"==Windows_NT ..... Since they are
not the same the statement is false

All NT class OS's return Windows_NT via the %OS% variable. This is mostly
useful to determine if you are running on an NT class OS or a Windows 9x OS.
Windows 9x systems don't return anything for the %OS% variable as it doesn't
exist.

Once you have determined what type of OS you are running on, you can then do
a more thorough check using the VER | FIND command.

Here's an example:

----- BEGIN EXAMPLE -----
@echo off
if not "%OS%"=="Windows_NT" goto :W9X

:: Now check for specific type of NT class OS...

set OSVER=NT
ver | find "2000" > nul && set OSVER=2000
ver | find "XP" > nul && set OSVER=XP
ver | find "2003" > nul && set OSVER=2003

echo Your Operating System is Windows %OSVER%

goto :eof

:W9X
:: from here on you can use Windows 9x specific commands
ver | find "98" > nul
if errorlevel 0 set OSVER=98
..
..
----- END EXAMPLE -----
 
G

Guest

A much easier/shorter way (compared to my last posting)
to run version-specific commands...
This only works in Windows 2000 / 2003 / XP.
My last posting (10/20/2003) works in all versions.

@FOR /F "TOKENS=3" %%A IN ('VER') DO IF %%A==2000 enter your command(s) here
@FOR /F "TOKENS=3" %%A IN ('VER') DO IF %%A==2003 enter your command(s) here
@FOR /F "TOKENS=3" %%A IN ('VER') DO IF %%A==XP enter your command(s) here


This script is only 3 lines (it may appear broken into more on screen).
Each line starts with @FOR and ends with here.

The 3rd token of the VER command's output is the Windows version (2000 or XP).

Replace "enter your command(s) here" with any command(s) you want.

Commands on the 1st line will only execute if you are running Windows 2000.
Commands on the 2nd line will only execute if you are running Windows 2003.
Commands on the 3rd line will only execute if you are running Windows XP.

Austin M. Horst
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top