PC Review


Reply
Thread Tools Rate Thread

Bonehead batch file question

 
 
Craig
Guest
Posts: n/a
 
      18th Oct 2003
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
 
Reply With Quote
 
 
 
 
Bill Stewart
Guest
Posts: n/a
 
      18th Oct 2003
Craig <(E-Mail Removed)> wrote:

> 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


 
Reply With Quote
 
Craig
Guest
Posts: n/a
 
      18th Oct 2003
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


>-----Original Message-----
>Craig <(E-Mail Removed)> wrote:
>
>> 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
>
>
>.
>

 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      18th Oct 2003
"Craig" schrieb:

>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
 
Reply With Quote
 
Ole
Guest
Posts: n/a
 
      20th Oct 2003
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

"Matthias Tacke" <(E-Mail Removed)> wrote in message
news:bms0q4$gue$00$(E-Mail Removed)...
"Craig" schrieb:

>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


 
Reply With Quote
 
=?Utf-8?B?QXVzdGluIE0uIEhvcnN0?=
Guest
Posts: n/a
 
      20th Oct 2003
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
 
Reply With Quote
 
Thanatos
Guest
Posts: n/a
 
      21st Oct 2003
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 -----


"Craig" <(E-Mail Removed)> wrote in message
news:02fc01c39589$1aaa9170$(E-Mail Removed)...
> 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



 
Reply With Quote
 
=?Utf-8?B?QXVzdGluIE0uIEhvcnN0?=
Guest
Posts: n/a
 
      23rd Oct 2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch file question Ronald S. Cook Microsoft VB .NET 3 13th Jul 2006 05:33 PM
Bonehead batchfile question Craig Microsoft Windows 2000 CMD Promt 16 13th Sep 2004 08:48 PM
bonehead question =?Utf-8?B?Y2Q=?= Microsoft Windows 2000 3 1st Jun 2004 04:26 AM
Bonehead question =?Utf-8?B?TmljayBN?= Microsoft Access Macros 2 27th Mar 2004 08:24 AM
Re: Bonehead batch file question Bill Stewart Microsoft Windows 2000 CMD Promt 0 21st Oct 2003 11:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:20 AM.