2

Based on this post for changing the output color of command prompt text in Windows 10, I'm looking for the best way to detect the OS version in a batch file IF statement.

How to echo with different colors in the Windows command line

From my understanding, colorized text was added in Windows 10 Threshold 2 November 18, 2015 (10.0.10586.11)

In Windows 10 version history, up until the Version 1607 (Anniversary Update), there was a major.minor.build.revision numbering scheme so I want the detection to be very granular.

https://en.wikipedia.org/wiki/Windows_10_version_history

I found this post which compares major.minor.build to a user input, but can't wrap my head around what it's doing for my needs.

https://github.com/LogicDaemon/PersonalUseScripts/blob/c3346df9c7c0f57b584b5c0238b8e8b76096c42c/cmd/superuser%20q%20891742/CheckWinVer.cmd

So based on that information, I'm trying to compare the detected version of windows to 10.0.10586.11 in a batch file.

If its greater than or equal to that version, colorize the text, if not use standard formatting.

This was my first attempt to colorize text in an IF statement. Although this appeared to work in Windows 10, it didn't work in Windows 8.1 because string "10." is less than "6.3"

FOR /F "tokens=4-5 delims=. " %%i in ('ver') DO SET Version=%%i.%%j
IF "%Version%" GEQ "10.0" (echo [92mHere is my text in green.[0m) ELSE (echo Here is my text)

Note the required escape characters are missing in the above sample

Community
  • 1
  • 1
TechFA
  • 129
  • 1
  • 9

1 Answers1

3

Compare numeric values instead of strings (e.g. Windows 8.1 returns 603):

FOR /F "tokens=4-5 delims=. " %%i in ('ver') DO SET /A _Version=%%i*100+%%j
IF %_Version% GEQ 1000 (echo [92mHere is my text in green.[0m) ELSE (echo Here is my text)

Edit. Instead of wikipedia, I'd use a primary source Windows 10 release information on Microsoft’s TechNet site.

FOR /F "tokens=4-7 delims=[]. " %%i in ('ver') DO (
  SET /A _MajorMinor = %%i * 100 + %%j
  set /A _Build      = %%k0 /10
  set /A _Revision   = %%l0 /10
)
set "_AE="        AE like Ansi Escape
IF %_MajorMinor% GEQ 1000 if %_Build% GTR 10586 ( set "_AE=Yes" ) else ( 
      if %_Build% EQU 10586 if %_Revision% GEQ 11 set "_AE=Yes" )
rem use the _AE flag
if defined _AE (echo [92mHere is my text in green.[0m) ELSE (echo Here is my text)

Used nested IFs (effective as of logical AND).

The set /A _Revision = %%l0 /10 construct used to avoid Missing operator on Windows 8 where the 7th token %%l results to an empty string.

JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • That seems like a reasonable approach to just get it working for Windows 8.1 but I want to get granular down to the sub-versioning numbers for unpatched Windows 10 – TechFA Mar 29 '17 at 21:40
  • Unless you can add a decimal point in there... For 10.0.10586.11, would need tokens 4-7 and the possibility of the missing operator also. Something like 10*100+0+10586+.11 How would I accomplish that? – TechFA Mar 29 '17 at 21:54
  • 1
    Save yourself the pain and frustration and use PowerShell instead. – Bill_Stewart Mar 30 '17 at 14:30
  • @Bill_Stewart I have no problem with PowerShell (pros >> cons) but the question **a)** is tagged *batch-file*, and **b)** is not about *colouring* output. – JosefZ Mar 30 '17 at 17:31
  • 2
    @JosefZ Your example just needs delims=[.] I was getting a missing operator due to ] at the end. – TechFA Mar 31 '17 at 04:32
  • @JosefZ is there a difference with []. vs [.] or was that a typo? I'm a newbie with delimiters – TechFA Mar 31 '17 at 14:21
  • @TechFA order of characters does not matter **excepting** default delimiters ( and ) **if** used ( then, a space must be placed last i.e. right before closing `"` double quote) Read more under _Delims_ and _Splitting a string that includes spaces_ headers in https://ss64.com/nt/for_f.html. – JosefZ Mar 31 '17 at 14:35