School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,920 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,773 people online right now. Registration is fast and FREE... Join Now!




Find What OS you are running, with a script

 

Find What OS you are running, with a script

Gorian

6 Nov, 2009 - 01:01 PM
Post #1

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
So, not sure what forum was best to put this....

What I want is to use a script to find out what os the program is run on, and save it to a variable that can be accessed by another program, or possibly the same one?

To distinguish between XP and Vista, I have the following for a batch file, however, it is not really clear enough, and only encompasses 2 of a lot of different windows OSs.

CODE

:: Script to find the OS Version
:: By Gorian
@echo off
cd ..

if %OS% == Windows_NT Goto Windows_NT

:Windows_NT
set pathXP==C:\Documents and Settings\%USERNAME%
set pathVista==C:\Users\%USERNAME%

if /i %USERPROFILE% == %pathXP% Goto XP
if /i %USERPROFILE% == %pathVista% Goto Vista
Goto Error

:XP
set OSVERSION=XP
Goto End

:Vista
set OSVERSION=Vista
Goto End

:Error
echo.
echo An Error has occured
echo.
pause
Goto End

:End
echo %OSVERSION%
exit /b




I know that I can use psinfo from the sysinternals to find it, and had code that parsed the output until only the OS version remained...

CODE

psinfo | grep "Kernel version" | gawk -F: "{ print $2 }" | gawk -F, "{ print $1 }" |gawk -F" " "{ print $3 }"


however, I could not seem to get this into a system variable, i tried different redirection operators > and | and such.... I would like to have in a simpler script, so I can have it as a variable that can be accessed by Batch Files.

I am not asking for code, but some guidance would be appreciated.

This post has been edited by Gorian: 6 Nov, 2009 - 01:02 PM

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Find What OS You Are Running, With A Script

6 Nov, 2009 - 01:29 PM
Post #2

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 14,919



Thanked: 517 times
Dream Kudos: 11525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Moved to Other Languages smile.gif
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

6 Nov, 2009 - 01:30 PM
Post #3

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
Cool. Thanks PsychoCoder. smile.gif That was my second choice, since I was using mostly batch files, but I figured our OS guys might know too.....

This post has been edited by Gorian: 6 Nov, 2009 - 01:31 PM
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

7 Nov, 2009 - 11:18 AM
Post #4

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
or apparently no one? If ya help me, I'll let ya hug gabehabe.....
User is offlineProfile CardPM
+Quote Post

Raynes

RE: Find What OS You Are Running, With A Script

7 Nov, 2009 - 10:28 PM
Post #5

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
(System/getProperty "os.name")

In Clojure. Of course that could translate to Java as well, since it's simply a Java method call from Clojure. :>
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

8 Nov, 2009 - 05:06 PM
Post #6

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
I don't know Clojure or Java.... Is it the variable callable from other files?
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

10 Nov, 2009 - 12:28 PM
Post #7

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
So, Here is my revised code, but it doesn't solve my problem...

CODE

:: Script to find the OS Version
:: By Gorian
@echo off
cd ..

if %OS% == Windows_NT Goto Windows_NT

:Windows_NT
set pathXP=C:\Documents and Settings\%USERNAME%
set pathVista=C:\Users\%USERNAME%

if /i  "%USERPROFILE%" == "%pathXP%" (Goto XP)
if /i  "%USERPROFILE%" == "%pathVista%" (Goto Vista)
Else (Goto Error)

:XP
set OSVERSION=XP
Goto End

:Vista
set OSVERSION=Vista
Goto End

:Error
echo.
echo An Error has occured
echo.
pause
Goto End

:End
::echo %OSVERSION%
exit /b



This post has been edited by Gorian: 10 Nov, 2009 - 12:28 PM
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

15 Nov, 2009 - 08:52 PM
Post #8

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
Wondering if anyone can answer... please? Offer still stands....

This post has been edited by Gorian: 15 Nov, 2009 - 08:52 PM
User is offlineProfile CardPM
+Quote Post

Raynes

RE: Find What OS You Are Running, With A Script

15 Nov, 2009 - 09:59 PM
Post #9

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
Maybe you would get an answer on stackoverflow.com? I wouldn't normally recommend for someone to go to a different site, but it's obvious you aren't getting any answers here.
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

15 Nov, 2009 - 11:42 PM
Post #10

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
I know. It is weird, and I love these guys too. sad.gif Makes me sad.
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

15 Nov, 2009 - 11:53 PM
Post #11

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
maybe if I was more clear? I chose to use Batch Files because they seemed easiest, but if someone can suggest something else that does not require me to learn a new language...

What I want is a script to find out what OS is being run, and store it to a variable so that it can be called again. This allows me to write scripts for copying, or running programs that call the right path based on the OS. For instance, %appdata% is different in Vista than in XP, and it can be annoying to path things like "%appdata%\..\..\Folder\Folder\file.ext"

With a batch file, I can call the script as call OS_Version.bat and all variables made with the set command are available to be called by the calling script. Plus, it seems to me, without getting complicated in C++, to start programs, one would do a system("Start File.exe"); or shell execute anyway, so it makes sense to use a batch file and save time and space....

This post has been edited by Gorian: 15 Nov, 2009 - 11:53 PM
User is offlineProfile CardPM
+Quote Post

olibenu

RE: Find What OS You Are Running, With A Script

16 Nov, 2009 - 01:00 AM
Post #12

D.I.C Regular
Group Icon

Joined: 31 Dec, 2008
Posts: 269



Thanked: 15 times
Dream Kudos: 350
My Contributions
since it's hard to do it with batch scripts, have u tried the registry from ur language. i'm sure it's stored somewhere
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

16 Nov, 2009 - 11:00 AM
Post #13

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
Ah, now that is a good idea.... Thank you. smile.gif Problem being, seems like a lot more work... confused.gif

This post has been edited by Gorian: 16 Nov, 2009 - 11:00 AM
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

16 Nov, 2009 - 06:36 PM
Post #14

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
Yay! I think I solved it. smile.gif

batch

@ECHO OFF


VER |find /i "Windows 95" > nul
IF NOT ERRORLEVEL 1 GOTO 95

VER |find /i "Windows 98" > nul
IF NOT ERRORLEVEL 1 GOTO 98

VER |find /i "Windows Millennium" > nul
IF NOT ERRORLEVEL 1 GOTO ME

VER | find "XP" > nul
IF %errorlevel% EQU 0 GOTO XP

VER | find "2000" > nul
IF %errorlevel% EQU 0 GOTO 2000

VER | find "NT" > nul
IF %errorlevel% EQU 0 GOTO NT

VER | find /i "Vista" > nul
IF %errorlevel% EQU 0 GOTO Vista

VER | find "7" > nul
IF %errorlevel% EQU 0 GOTO 7

Else Goto ERROR



:95
Set OS_Version=Windows 95
GOTO :EOF

:98
set OS_Version=Windows 98
Goto EOF

:ME
set OS_Version=Windows ME
Goto EOF

:XP
Set OS_Version=Windows XP
Goto EOF

:2000
Set OS_Version=Windows 2000
Goto EOF

:NT
Set OS_Version=Windows NT
Goto EOF

:Vista
Set OS_Version=Windows Vista
Goto EOF

:7
Set OS_Version
Goto EOF

:ERROR
cls
Echo.
Echo Sorry. Windows has encountered a fatal script problem and must terminate.
Echo The Operating System you are running is unknown.
Echo.
Goto EOF

:EOF (End-of-file)
cls
exit /b


This post has been edited by Gorian: 16 Nov, 2009 - 06:36 PM
User is offlineProfile CardPM
+Quote Post

KYA

RE: Find What OS You Are Running, With A Script

16 Nov, 2009 - 06:57 PM
Post #15

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,489



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Looks like you already got it, but if you wanted to explore other avenues, you could use some of the Windows functions for more specific information:

CODE

int main () {
    OSVERSIONINFO info;
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    if(!GetVersionEx(&info)){cout << "Failed!\n\n";}

    cout << "Build number: " << info.dwBuildNumber << endl;
    cout << "Major versionr: " << info.dwMajorVersion << endl;
    cout << "Minor version: " << info.dwMinorVersion << endl;
    cout << "Platform id: " << info.dwPlatformId << endl;
    cout << "Size of this: " << info.dwOSVersionInfoSize << endl;
    return 0;
}



from here
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

16 Nov, 2009 - 10:12 PM
Post #16

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
Thanks KYA! wub.gif

I need to fix it anyway though, it doesn't work with Vista, and probably not 7.

I may look into that. smile.gif
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Find What OS You Are Running, With A Script

17 Nov, 2009 - 04:20 PM
Post #17

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 711



Thanked: 8 times
Dream Kudos: 125
My Contributions
Okay, So last revision? I think I covered the Major ones.... Not any Server OS's though... blink.gif but it covers Vista and 7 in addition

batch

:: OS Version - To Find what version of Windows is running

@ECHO OFF


::Find Windows

::Windows 95
VER | find /i "4.0" > nul
IF NOT ERRORLEVEL 1 GOTO 95

::Windows 98
VER | find /i "4.10" > nul
IF NOT ERRORLEVEL 1 GOTO 98

::Windows ME
VER | find /i "4.9" > nul
IF NOT ERRORLEVEL 1 GOTO ME

::Windows 2000
VER | find /i "5.0" > nul
IF NOT ERRORLEVEL 1 GOTO 2000

::Windows XP
VER | find "5.1" > nul
IF %errorlevel% == 0 GOTO XP

::Windows Vista
VER | find /i "6.0" > nul
IF %errorlevel% == 0 GOTO Vista

::Windows 7
VER | find "6.1" > nul
IF %errorlevel% == 0 GOTO 7

Else Goto ERROR



:95
Set OS_Version=Windows 95
GOTO :EOF

:98
set OS_Version=Windows 98
Goto EOF

:ME
set OS_Version=Windows ME
Goto EOF

:2000
Set OS_Version=Windows 2000
Goto EOF

:XP
Set OS_Version=Windows XP
Goto EOF

:Vista
Set OS_Version=Windows Vista
Goto EOF

:7
Set OS_Version=Windows 7
Goto EOF

:ERROR
cls
Echo.
Echo Sorry. Windows has encountered a fatal script problem and must terminate.
Echo The Operating System you are running is unknown.
Echo.
Goto EOF

:EOF
cls
exit /b


This post has been edited by Gorian: 17 Nov, 2009 - 04:38 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:49AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month