Detect Windows OS then execute an external BAT file, but failed

Hello Experts,

Found a BAT program online and I currently has 3 OSs {WINXP, 7 and 8.1} and want to check on the OS version and executed if it applied to the OS I have, but none are run.
{ http://www.grimadmin.com/article.php/batchfile-easy-way-to-detect-os-version }

Thank you,

@ECHO

ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto ver_2000

ver | find "NT" > nul
if %ERRORLEVEL% == 0 goto ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

FOR /F "delims=: tokens=2" %%i IN ('systeminfo 2^>NUL ^| find "OS Name"') DO set vers=%%i

echo %vers% | find "Windows 8" > nul
if %ERRORLEVEL% == 0 goto ver_8

echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_8
CALL ping.bat
echo Windows 8

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

:ver_2008
:Run Windows Server 2008 specific commands here.
echo Windows Server 2008
goto exit

:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit

:ver_2003
:Run Windows Server 2003 specific commands here.
echo Windows Server 2003
goto exit

:ver_xp
:Run Windows XP specific commands here.
echo Windows XP
goto exit

:ver_2000
:Run Windows 2000 specific commands here.
echo Windows 2000
goto exit

:ver_nt
:Run Windows NT specific commands here.
echo Windows NT
goto exit

:warnthenexit
echo Machine undetermined.

:exit

Open in new window

Lonelygirl_2012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
Lots of ways but you could try this one if you want:

Steve

@echo off
cscript //nologo GetOS.VBS | find "Windows 7" && goto Windows7
cscript //nologo GetOS.VBS | find "Windows 8.1" && goto Windows 81
cscript //nologo GetOS.VBS | find "Windows 8" && goto Windows8
cscript //nologo GetOS.VBS | find "Vista" && goto Vista
cscript //nologo GetOS.VBS | find "XP" &&  goto XP
echo Other OS found
pause

:Windows8
  echo Windows 8
  pause
exit /b
:Windows81
  echo Windows 8.1
  pause
exit /b
:Windows7
  echo Windows 7
  pause
exit /b
:Vista
  echo Windows Vista
  pause
exit /b
:XP
  echo Windows XP
  pause
exit /b

Open in new window


Save a getOS.vbs:
Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

wscript.echo GetOS

' **************
' FUNCTION GetOS
' **************
' This function returns the OS version and service pack level
' The word "Service Pack " is replaced with "SP" and "Microsoft " removed to make it less long.

Function GetOS
  Set OpSys = objWMIService.instancesof("Win32_OperatingSystem")
  For Each Item In OpSys
    Caption = item.Caption
  Next
  GetOS=trim(Replace(Replace(Caption,"Microsoft",""),"  "," "))
End Function

Open in new window

GetOS.vbs
os.cmd
0
Bill PrewCommented:
While there are more efficient ways to go about this, the code you are using should work.  A couple of thoughts on that.

Is it displaying the "Machine undertermined" message?  If not, what is happening?

Can you post up the output of the VER command on the system it fails on, as well as the SYSTEMINFO command (at least the "OS NAME" line.

I also noticed a potential problem in the following block:

:ver_8
CALL ping.bat
echo Windows 8

Open in new window


First, does the PING.BAT file turn around and do a PING command in it?  If so you may have a loop there that will never end, where the PING.BAT file just keeps calling itself.  Try changing the file name to say MYPING.BAT and change the CALL to that name.

Also, at the end of the "ver_8" block, you seem to be missing a "goto exit", so you would fall into the "ver_7" block.

~bp
0
Bill PrewCommented:
Here is a slightly leaner version using a similar basic approach to what you were using, since you are only really interested in 3 versions.  WMIC is a bit faster than using SYSTEMINFO also, since we can query just what we want.

@echo off

for /f "skip=2 tokens=2-5 delims=," %%A in ('wmic os get caption^,osarchitecture^,ostype^,version /format:csv') do (
  set "os.caption=%%A"
  set "os.arch=%%B"
  set "os.type=%%C"
  set "os.version=%%D"
)

echo %os.caption%|find /i "xp">NUL && goto ver_xp
echo %os.caption%|find /i "windows 7">NUL && goto ver_7
echo %os.caption%|find /i "windows 8">NUL && goto ver_8
echo Machine undetermined - "%os.caption%"
goto exit

:ver_8
:Run Windows 8 specific commands here.
echo Windows 8
goto exit

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

:ver_xp
:Run Windows XP specific commands here.
echo Windows xp
goto exit

:exit                                  
exit /b

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lonelygirl_2012Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.