<http://www.netikka.net/tsneti/info/tscmd004.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Mon 26-Jan-2015 19:29:29

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



4} How can I test is it on MSDOS/Win3..9x/Me or NT/2000/XP?

This is one way how to test it:
  @echo off
  set is_nt=false
  echo.>nul & set is_nt=true
  :: Show the result
  echo is_nt=%is_nt%
It is based on the fact that in NT/2000/XP CMD.EXE "&" can be used to separate multiple commands on one command line while in MSDOS/Win3..9x/Me COMMAND.COM the "&" is not recognized.

Another, very similar option is based on the maximum length of a label in the various systems:
  @echo off
  ::
  goto _123456789
  :_1234567
  set is_nt=false
  goto _tell
  :_123456789
  set is_nt=true
  goto _tell
  ::
  :_tell
  echo is_nt=%is_nt%

The output on an XP would be
  C:\_D\TEST>cmdfaq
  is_nt=true

A third option is the following. The most common. It assumes that you have not tampered with the predefined system environment variables for Windows XP. Their values can be changed by the user, even if it is highly unrecommended.
  @echo off
  if [%OS%]==[Windows_NT] goto _xp
  :: whatever MSDOS..95/98/ME-syntax commands
  goto _end
  :_xp
  :: whatever NT/2000/XP-syntax commands
  :_end

A more specific alternative testing for XP in particular is the following
  @echo off
  set is_xp=false
  ver|find "Microsoft Windows XP"
  if %errorlevel% EQU 0 set is_xp=true
  echo is_xp=%is_xp%
  set is_xp=

The output on an XP could be e.g.
  C:\_D\TEST>cmdfaq
  Microsoft Windows XP [Version 5.1.2600]
  is_xp=true
The solution has the added advantage that one can build a more comprehensive identification on this alternative if one first maps what the various Windows or MS-DOS versions will return for VER.

For a different, comprehensive identification of the particular Windows_NT category, search http://www.commandline.co.uk/lib/treeview/index.php by Ritchie Lawrence for the GetOS function.

James Pang wrote:
I want to use below command to set the output string of find into
  variable 'v' ver | find "Windows" | set /p v=
but it doesn't work!!! anybody has any idea?

My response: That is a problem that is a superficially easy
  @echo off & setlocal enableextensions
  if [%OS%]==[Windows_NT] echo NT/2000/XP...
  for /f "tokens=2 delims=[]" %%v in ('ver') do set ver_=%%v
  echo %ver_%
  endlocal & goto :EOF

Which would give e.g.
  C:\_D\TEST>cmdfaq
  NT/2000/XP...
  Version 5.1.2600
However, that is somewhat of a circular deduction. We already have advance knowledge what the OS is when using the above! Note that 'ver' output fields can differ between Windows versions. The more fundamental question is how does one generally find out in a script which OS? That problem has been tackled by Ritchie Lawrence in GetOS.

Here is my rendition, partly incomplete and (naturally) not tested for all the OSes.
  @echo off
  set my_os=
  ::
  echo Test|find "Fail">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Pre MS-DOS 6.0
  if not "%my_os%"=="" goto _show
  ::
  ver|find "6.22">nul
  if errorlevel 0 if not errorlevel 1 set my_os=MS-DOS 6.22
  ::
  ver|find "Windows 95.">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows95
  ::
  if not "%OS%"=="Windows_NT" goto _show
  ::
  net config workstation|find /i "Software version"|find "NT40">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows NT 4.0
  ::
  net config workstation|find /i "Software version"|find "2000">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows 2000
  ::
  net config workstation|find /i "Software version"|find "2002">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows XP
  ::
  net config workstation|find /i "Software version"|find "2003">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows 2003
  ::
  ver|find " 6.0">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Vista
  ::
  ver|find " 6.1">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows 7
  ::
  net config workstation|find /i "Software version"|find "Windows 7 Professional">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows 7 Professional
  ::
  ver|find " 6.2">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows 8
  ::
  ver|find " 6.3">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows 8.1
  ::
  ver|find " 6.4">nul
  if errorlevel 0 if not errorlevel 1 set my_os=Windows 10
  ::
  :_show
  if "%my_os%"=="" set my_os=Unknown
  echo %my_os%
  for %%v in (my_os) do set %%v=

The output could be e.g.
  C:\_D\TEST>cmdfaq
  Windows XP

Another option:
  @echo off & setlocal enableextensions
  set xp_=
  systeminfo|find "Microsoft Windows XP">nul
  if %errorlevel% EQU 0 set xp_=true
  if defined xp_ (
    echo XP
    ) else (
    echo Not XP)
  endlocal & goto :EOF

  C:\_D\TEST>cmdfaq
  XP

Or
  @echo off & setlocal enableextensions
  for /f "tokens=2 delims=:" %%a in (
    'systeminfo^|find /i "OS Name:"') do (
    set osname_=%%a)
  for /f "tokens=* delims= " %%a in (
    'echo %osname_%') do set osname_=%%a
  echo %osname_%
  endlocal & goto :EOF

  C:\_D\TEST>cmdfaq
  Microsoft Windows XP Professional

Or
  @echo off & setlocal enableextensions
  ::
  :: Get the Operating System
  for /f "tokens=2,*" %%i in (
  'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName^
    ^| find "REG_SZ"') do set prod_=%%j
  echo.%prod_%|find "Microsoft Windows XP">nul
  if %errorlevel% EQU 0 set xp_=true
  if defined xp_ (
    echo XP
    ) else (
    echo Not XP)
  endlocal & goto :EOF

  C:\_D\TEST>cmdfaq
  XP

A VBScript-aided solution:
  @echo off & setlocal enableextensions
  ::
  :: Build a Visual Basic Script and run it
  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  findstr "'%skip%VBS" "%~f0" > "%vbs_%"
  for /f "tokens=*" %%a in ('cscript //nologo "%vbs_%"') do set osName_=%%a
  ::
  :: Clean up
  for %%f in ("%vbs_%") do if exist %%f del %%f
  ::
  echo osName_=%osName_%
  ::
  endlocal & goto :EOF
  '
  'The Visual Basic Script 'VBS
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 'VBS
  Set colOSes = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem") 'VBS
  For Each objOS in colOSes 'VBS
    Wscript.Echo objOS.Caption 'VBS
  Next 'VBS

  C:\_M>C:\_D\TEST\CMDFAQ.CMD
  osName_=Microsoft Windows XP Professional

Incidentally, for a quick glance in the Windows GUI (Graphical User Interface) you can use in the CLI (Command Line Interface)

winver


References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups 8 Mar 2009 11:53:20 [T]
  List Operating System and Service Pack Information Microsoft TechNet Script Repository
  Wikipedia Ver command version list