0

I'm noob it's my first question, but after searching tons of answers here I still can't quite get what I want. So the problem: need to get windows version ,and edition and pass it to variable to run findstr on it afterwards I get the variable with the edition however findstr can't use it to find string in txt file.

what I have done: I'm using this code to get the ver and save it as var

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do ( 
    set winEdition=%%G
    )
echo !winEdition!
endlocal
goto :eof

Output: Microsoft Windows 7 Enterprise

however if I run IF statement or findstr with !winEdition! var I get no result with IF and "string not found" with findstr also if I echo the var to a txt file I get "牣獯景⁴楗摮睯⁳‷湅整灲楲敳†਍" in the txt file so I think it's encoding problem, but I can't find a way to fix it. More details: the full code suppose to take the var from the code above, search for the string in txt file and return the next line full code:

@echo OFF
setlocal EnableDelayedExpansion
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
    set winEdition=%%G
    )
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"!winEdition!" serials.txt') do (
   set /A after=%%a+1
   set "numbers=!numbers!!after!: "
)
rem Search for the lines
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
    echo %%b 
    )
endlocal
goto :eof

The second part of the code works and tested with string and with var I want to automate it further. Thank you for help I am also open to alternative ways to get the result!

TerraBys
  • 26
  • 5
  • Active code page: 862 – TerraBys Aug 09 '17 at 13:05
  • I get the correct output using `chcp 862` at top of batch file and on inserting before `endlocal` the command lines `echo "%winEdition%"` and `echo %winEdition% | findstr /C:Windows` and `if "%winEdition%" == "Microsoft Windows 7 Enterprise " echo Yes, it is Windows 7`. Please note the space character after `Enterprise`. And the batch file must be saved as ANSI file and not as Unicode file. – Mofi Aug 09 '17 at 13:18
  • Insert before `set numbers=` the command line `if "%winEdition:~-1%" == " " set "winEdition=%winEdition:~0,-1%"` to remove the trailing space character. And on command `findstr` use additionally `/L` to explicitly request interpreting the search string as literal string and not as regular expression. – Mofi Aug 09 '17 at 13:29
  • tried both of the suggestions still nothing output: `C:\Script>chcp 862 Active code page: 862 Microsoft Windows 7 Enterprise FINDSTR: No search strings Active code page: 862` the output of "Microsoft Windows 7..." is echo of `%WinEdition%` after second suggestion. – TerraBys Aug 09 '17 at 13:48
  • Possible duplicate of [Get Windows version in a batch file](https://stackoverflow.com/questions/13212033/get-windows-version-in-a-batch-file) –  Aug 09 '17 at 15:05
  • as I said I tried most of the methods already the one with `ver` works however it only gives me the version I need edition too (home, pro, etc.) – TerraBys Aug 09 '17 at 15:42
  • It looks like the real issue is not getting the Windows version string as this works. The problem is the contents of file `serials.txt` or its encoding as `findstr` can't find the Windows version string in that file. Without having this file the issue is most likely not reproducible by us.I created a `serials.txt` with Windows version strings inside and an individual text below each version string and your second batch code with the additional remove trailing space character command line worked fine on Windows 7 and even on Windows XP. – Mofi Aug 09 '17 at 17:23
  • @TerraBys Is it possible that your batch file contains trailing whitespaces on line with command `set`? I suggest to use `set "variable=value"` syntax as described in detail on answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564), i.e. `set "winEdition=%%G"` and `set "numbers="`. Which encoding is used for `serials.txt`? Could you add to your question some lines from `serials.txt` with pseudo serial strings? – Mofi Aug 09 '17 at 17:29
  • @Mofi Thanks for the help if you could post the working code of the first part that would be great, any way I found another way to find the version ,and declare it as a variable, you can see my answer for details also details of `serials.txt` – TerraBys Aug 09 '17 at 18:00

2 Answers2

1

wmic prints some additional "empty" lines (1), which overwrites your value (watch with echo on)

@echo off
setlocal 
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do ( 
    if not defined winEdition set winEdition=%%G
    )
echo %winEdition%

(1) on screen they appear to be empty, but technically they aren't. They contain a crippeled line break, which causes a lot of headache...

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Sorry Stephan, but this is not the problem here. The posted code works and outputs the string `Microsoft Windows 7 Enterprise` as TerraBys wrote. What does not work is the code not posted in question. The code works because of using `"tokens=2 delims=="` and __wmic__ option `/value` which results in only 1 line can be processed by __for__ which has a second token. See my answer on [%date% produces different result in batch file when run from Scheduled Tasks in Server 2016](https://stackoverflow.com/a/44670322/3074564) why code posted by TerraBys works. – Mofi Aug 09 '17 at 13:03
0

Hi again I found the answer!!! So I just used a different command to find the win version, and edition thanks to @LotPings, it's a bit slower than wmic but it works consistently every time. Code:

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=4-6" %%a in ('"systeminfo | find /i "OS Name""') do (
    set "ver=%%a %%b %%c"
    )

set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!ver!" serials.txt') do (
   set /A after=%%a+1
   set "numbers=!numbers!!after!: "
)

for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
    echo %%b 
    )

if "%ver%" equ "Windows 10 Pro"  echo YES 
endlocal
goto :eof

Thank you all for help, and suggestions!

ps. the serials.txt is just a normal text file created with notepad encoding UTF-8 and the content is just:

Windows 10 Pro  
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Windows 7 Pro...

and IF is just another check that it works in original code the problem is not with the second part nor with serials.txt because I couldn't compare the !winEdition! with IF statement.

BTW found the problem with my original solution thx to the one I found above the problem was wrong token= solution:

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=3-5 delims== " %%A in ('wmic os get Caption /value ^| find "Windows" ') do (
    set "winVer=%%A %%B %%C"
    )
echo !winVer!
set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!winVer!" serials.txt') do (
   set /A after=%%a+1
   set "numbers=!numbers!!after!: "
    )
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
    echo %%b 
    )

endlocal
goto :eof
TerraBys
  • 26
  • 5