Computing.Net > Forums > Disk Operating System > how to find OS in batch file

how to find OS in batch file

Reply to Message Icon

Original Message
Name: Shree
Date: August 6, 2002 at 07:28:39 Pacific
Subject: how to find OS in batch file
Comment:

i want to run a program based on running operating system. can any one help me to find which operating system is running (95/98/NT/2000).


Report Offensive Message For Removal

Response Number 1
Name: astroraptor
Date: August 6, 2002 at 07:50:04 Pacific
Subject: how to find OS in batch file
Reply: (edit)

ECHO.%OS%

This works under Windows NT/2000/XP


Report Offensive Follow Up For Removal

Response Number 2
Name: Michael
Date: August 6, 2002 at 09:11:13 Pacific
Subject: how to find OS in batch file
Reply: (edit)

Also the VER command will work also.

-Michael


Report Offensive Follow Up For Removal

Response Number 3
Name: Secret_Doom
Date: August 6, 2002 at 11:19:35 Pacific
Subject: how to find OS in batch file
Reply: (edit)

NT systems have the %OS% variable set by default to 'Windows_NT', and non-NT systems don't. So, if you just want to act depending if the OS is NT (Win NT4,2000,XP) or non-NT (Win9x, Dos 6.22), this will do it:

if "%OS%"=="Windows_NT" goto NT
echo This is non-NT!
goto eof
:NT
echo This is NT!
:eof

However, if you need the batch file to act in a certain way for each OS, the best would be to check the return of the VER command.

Something like this would be required:

ver > %TEMP%.\T1.DAT
FIND "MS-DOS" < %TEMP%.\T1.DAT > nul
if not errorlevel=1 goto dos
FIND "Windows 95" < %TEMP%.\T1.DAT > nul
if not errorlevel=1 goto win95
FIND "Windows 98" < %TEMP%.\T1.DAT > nul
if not errorlevel=1 goto win98
FIND "Windows 2000" < %TEMP%.\T1.DAT > nul
if not errorlevel=1 goto win2000
FIND "Windows XP" < %TEMP%.\T1.DAT > nul
if not errorlevel=1 goto winXP
echo FATAL ERROR: could not detect your Operating System.
goto end
:: Here goes the part with the labels :dos
:: :win98 :win2000 etc
:end
if exist %TEMP%.\T1.DAT del %TEMP%.\T1.DAT

PS: I used a temporary file and all these lower-than characters intead of "VER|FIND..." because it's faster.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: how to find OS in batch file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software