Find OS

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
happiness
Posts: 6
Joined: 08 Mar 2013 18:02
Location: Dinnington, Newcastle upon Tyne, UK
Contact:

Find OS

#1 Post by happiness » 11 Mar 2013 17:19

Evening all,

I'm very new to writing batch files and need a bit of help. I've "borrowed" some code off the internet to find the OS and now I want to add a "goto" command in but I can't seem to get it to work. Where am I going wrong?

Original code:

Code: Select all

::Identify OS
ver | find /i "version 6.2." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 8
ver | find /i "version 6.1." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 7
ver | find /i "version 6.0." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows Vista
ver | find /i "version 5.1." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows XP
ver | find /i "version 5.2." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 2003
ver | find /i "Windows 2000" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 2000
ver | find /i "Windows NT" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows NT
ver | find /i ">Windows ME" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows ME
ver | find /i "Windows 98" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 98
ver | find /i "Windows 95" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 95

::Identify bit
IF NOT EXIST "%SYSTEMDRIVE%\Program Files (x86)" set $VERSIONBIT=32 bit
IF EXIST "%SYSTEMDRIVE%\Program Files (x86)" set $VERSIONBIT=64 bit

::Display result
echo %$VERSIONWINDOWS% %$VERSIONBIT%
echo.
pause


I'm now trying to add in, after "pause", the "goto command of:

Code: Select all

if %$VERSIONWINDOWS%==Windows 7 goto :ver_7

:ver_8
echo Windows 8

:ver_7
echo Windows 7


I'm sure it's simple but I have no idea what's wrong. I've tried every version of the goto command I can think of!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find OS

#2 Post by Squashman » 11 Mar 2013 17:50

Put double quotes around the values you are comparing.

happiness
Posts: 6
Joined: 08 Mar 2013 18:02
Location: Dinnington, Newcastle upon Tyne, UK
Contact:

Re: Find OS

#3 Post by happiness » 11 Mar 2013 18:11

Squashman wrote:Put double quotes around the values you are comparing.

Like a charm, thanks!!

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Find OS

#4 Post by Aacini » 11 Mar 2013 21:10

I like it! :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion

::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8) do (
   if "!Version!" equ "this" (
      set Version=Windows %%a
   ) else if "!ver: %%a=!" neq "%ver%" (
      set Version=this
   )
)

::Identify bit
if exist "%SYSTEMDRIVE%\Program Files (x86)" (
   set Type=64 bit
) else (
   set Type=32 bit
)

::Display result
echo %Version% %Type%
echo/
pause


::Goto right version
goto %Version: =_%


:Windows_8
echo Windows 8

:Windows_7
echo Windows 7

Antonio

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Find OS

#5 Post by Ocalabob » 11 Mar 2013 23:00

@Antonio
I like it as well! Two quick tries with your script on two OS's I get:

Windows XP 32 bit
Windows 7 64 bit

Nice work!

crobertson
Posts: 20
Joined: 25 May 2012 12:34

Re: Find OS

#6 Post by crobertson » 10 Nov 2014 19:18

Aacini, is there a way to just save the Version number so I can just go to version

@echo off
setlocal EnableDelayedExpansion

::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8 6.3=8.1) do (set Version=%%a)

::Identify bit
IF NOT EXIST "%SYSTEMDRIVE%\Program Files (x86)" set bits=32
IF EXIST "%SYSTEMDRIVE%\Program Files (x86)" set bits=64

echo %Version% %bits%
echo.
pause
goto %version%

:XP
echo XP
pause goto end

:Vista
echo Vista
pause goto end

:7
echo Win7
pause goto end

:8
echo Win8
pause goto end

:8.1
echo Win 8.1
pause goto end

:end

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Find OS

#7 Post by Compo » 15 Nov 2014 15:16

Here's an attempt to identify XP and above...

Code: Select all

@ECHO OFF
SETLOCAL
SET "_="
FOR /F "TOKENS=1* DELIMS=[" %%A IN ('VER') DO CALL :SUB %%B
SET xNN=x%PROCESSOR_ARCHITECTURE:~-2%
IF %xNN% EQU x86 (IF DEFINED PROCESSOR_ARCHITEW6432 SET xNN=x64)
NET ACCOUNTS|FIND "WORK">NUL||SET "_=T"
IF %OSV% LSS 6 GOTO XP2K3
IF %OSV% LSS 6.1 GOTO VST2K8
IF %OSV% LSS 6.2 GOTO W72K8R2
IF %OSV% LSS 6.3 GOTO W82K12
IF %OSV% LSS 6.4 GOTO W812K12R2
REM Windows 10 Commands below here
ECHO(Windows 10 [Technical Preview] %xNN%)
GOTO ENDIT

:W812K12R2
REM Windows 8.1 or Server 2012 R2 Commands below here
IF DEFINED _ (ECHO(Windows Server 2012 R2 %xNN%) ELSE (
   ECHO(Windows 8.1 %xNN%)
GOTO ENDIT

:W82K12
REM Windows 8 or Server 2012 Commands below here
IF DEFINED _ (ECHO(Windows Server 2012 %xNN%) ELSE (
   ECHO(Windows 8 %xNN%)
GOTO ENDIT

:W72K8R2
REM Windows 7 or Server 2008 R2 Commands below here
IF DEFINED _ (ECHO(Windows Server 2008 R2 %xNN%) ELSE (
   ECHO(Windows 7 %xNN%)
GOTO ENDIT

:VST2K8
REM Windows Vista or Server 2008 Commands below here
IF DEFINED _ (ECHO(Windows Server 2008 %xNN%) ELSE (
   ECHO(Windows Vista %xNN%)
GOTO ENDIT

:XP2K3
REM Windows XP or Server 2003 Commands below here
IF DEFINED _ (ECHO(Windows Server 2003 %xNN%) ELSE (
   ECHO(Windows XP %xNN%)

:ENDIT
PING -n 4 127.0.0.1 1>NUL
GOTO :EOF

   :SUB
   SET OSV=%2
   SET OSV=%OSV:~,3%
   IF %OSV% GEQ 5.1 (IF %OSV% LEQ 6.4 GOTO :EOF)
   ECHO(UNSUPPORTED OPERATING SYSTEM
   PING -n 4 127.0.0.1 1>NUL
   EXIT

Post Reply