I am a bit unexperienced with CMD and hope someone here can help me.

In one old installation script of mine I now need to check if .NET 3.5 is installed, and then either install the 3.5 package or move on with the script depending on the result.

Now I have some trouble trying to check for the installed version in my Script.

I use this reg query to look for the installed version:

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP" | Find "3.5"

which outputs a single line, if 3.5 is installed, or nothing if it isn't:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5

Now my idea was, to create a variable and fill the value with the output seen above.

Unfortunately something like:

set DNFound=reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP" | Find "3.5"
if not exist %DNFound%
(
 install framework
)

doesn't work, because the output wont get saved in the variable.

I have researched a bit and seen people go for a loop in this format to get single values from the reg query output into a variable (sample code):

 for /f "tokens=3" %%x in ('reg query HKLM\System\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall') do set FWSTATUS=%%x

However I get a syntax error when I try to do the same, because of the | Find "3.5" part in my reg query.

2 Answers

0
Gerhard Barnard On Best Solutions

I would suggest you use errorlevel to determine if the string returned successfully.

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP" | Find "3.5"
if errorlevel 1 install framework

What happens is find returns an errorlevel of 1 (no match) 0 match found. This way you do not require a variable to be set.

The above can be expanded into mutlple commands:

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP" | Find "3.5"
if errorlevel 1 (
    echo Framework not found
    install framework
    echo do something else
)

As for the for loop issue, as already mentioned by @LotPings in a comment, you need to escape the pipe ^|:

for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP" ^| Find "3.5" 2^>nul) do set "var=%%i"
echo %var%
0
Compo On

As an addition to your question, because it may be relevant to future users, you should be able to output all of the installed versions using a method similar to this:

@Echo Off
SetLocal EnableDelayedExpansion
Set "rk=HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP"
Set/A "vi=ri=0"

For /F "Tokens=3*" %%A In ('Reg Query "%rk%" /F v /K') Do If "%%B"=="" (
    If Not "%%~xA"=="" (Set/A "vi+=1"
        For /F "Tokens=2*" %%C In (
            'Reg Query "%rk%\%%~nxA" /V Version 2^>Nul^|Find /V "\"'
        ) Do Set "_v!vi!=%%D"
    ) Else (Set/A "ri+=1"
        For /F "Tokens=2*" %%E In (
            'Reg Query "%rk%\%%~nxA\Full" /V Release 2^>Nul^|Find /V "\"'
        ) Do Set/A "_r!ri!=%%F"))
If %ri% Gtr 0 (Set/A "vi+=1"
    For /F "Tokens=2" %%A In ('FindStr/B "!_r%ri%!" "%~f0"'
    ) Do Set "_v%vi%=%%A")

If %vi% Gtr 0 For /F "Tokens=1* Delims==" %%A In ('Set _v') Do Echo([%%B]

Pause
GoTo :EOF

Rem Later Version Table - do not remove
378389 4.5
378675 4.5.1
378758 4.5.1
379893 4.5.2
393295 4.6
393297 4.6
394254 4.6.1
394271 4.6.1
394802 4.6.2
394806 4.6.2
460798 4.7
460805 4.7
461308 4.7.1
461310 4.7.1
461808 4.7.2
461814 4.7.2
528040 4.8
528049 4.8

When a new version comes out, you simply update the content at the bottom with the relevant information, such that the detection will remain up to date.