(Contents)(Previous)(Next)

Windows Version Detector

Windows Detection Based on Environment Variables

The following statements in a batch file detect which version of Windows is currently running on the computer, without the use of temporary files. The environment variables (with lower case names) windir, winbootdir, and os are set by the Windows operating system during startup.

Code Listing 6. Windows Detection in Batch Files

@ECHO OFF
IF '%OS%' == 'Windows_NT' GOTO Done
SET windir=
SET windbootdir=
if '%windir%' == '' SET OS=DOS
if '%OS%' == '' IF '%Winbootdir%'=='' SET OS=Windows_3.x
if '%OS%' == '' SET OS=Windows_95
:Done
ECHO Operating system is %OS%

The execution of the batch file relies on the fact that if a user-created environment variable WINDIR exists, the first SET command deletes the contents. The Windows-created 'windir' variable still exists. Upper and lower cases are distinguished in environment variables names is batch files, but the DOS SET command converts all variable names to upper case. So, there is no way for a user to create, change, or delete the 'windir' (all lower case) variable using DOS commands.


(Next)