View unanswered posts | View active topics It is currently 26 Nov 2014 12:26



Post new topic Reply to topic  [ 7 posts ] 
Find OS 
Author Message

Joined: 08 Mar 2013 18:02
Posts: 6
Location: Dinnington, Newcastle upon Tyne, UK
Post Find OS
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:
::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:
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!


11 Mar 2013 17:19
Profile ICQ WWW

Joined: 23 Dec 2011 13:59
Posts: 2017
Post Re: Find OS
Put double quotes around the values you are comparing.


11 Mar 2013 17:50
Profile

Joined: 08 Mar 2013 18:02
Posts: 6
Location: Dinnington, Newcastle upon Tyne, UK
Post Re: Find OS
Squashman wrote:
Put double quotes around the values you are comparing.

Like a charm, thanks!!


11 Mar 2013 18:11
Profile ICQ WWW
Expert

Joined: 06 Dec 2011 22:15
Posts: 776
Location: México City, México
Post Re: Find OS
I like it! :D

Code:
@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


11 Mar 2013 21:10
Profile

Joined: 24 Dec 2010 12:16
Posts: 79
Location: Micanopy Florida
Post Re: Find OS
@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!


11 Mar 2013 23:00
Profile

Joined: 25 May 2012 12:34
Posts: 13
Post Re: Find OS
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


10 Nov 2014 19:18
Profile

Joined: 21 Mar 2014 08:50
Posts: 115
Post Re: Find OS
Here's an attempt to identify XP and above...
Code:
@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


15 Nov 2014 15:16
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot], zagix and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.