0

I've written some batch code which checks a users system for .NET Core Desktop Runtime v5. If the users system has it, then an appropriate message is displayed. If they don't, they are presented with the opportunity to download it via the correct hyperlink.

Current Code:

@echo off
Title Mod Studio 2 .NET Core Check
:: Check for .Net Core v5
echo:
echo Checking your computer for .Net Core v5 which is required by Mod Studio 2 ...
echo:

setlocal enabledelayedexpansion
set OutputFilename=%TEMP%\MS2netcorecheck.log

:: Send dotnet results to output log file and find relevant core version
dotnet --list-runtimes | find /i "Microsoft.WindowsDesktop.App 5" | find /i /V "-rc" >nul 2>&1
:: If search result is positive then ...
IF %ErrorLevel% EQU 0 (
    DEL /F /Q %TEMP%\MS2netcorecheck.log
    @dotnet --list-runtimes > "!OutputFilename!"
    echo .Net Core v5 is already installed; therefore you do not need to download it.
    echo:
    echo Please press any key to close this window.
    echo:
    pause >nul
    Goto :SkipDotNet
) else (
    DEL /F /Q %TEMP%\MS2netcorecheck.log
    @dotnet --list-runtimes > "!OutputFilename!"
    echo .Net Core v5 is NOT installed.
    echo:
    echo Please download and install .Net Core v5 now ...
    echo:
    echo Please do NOT click on the close button as this will not load the download page.
    echo:
    echo To be taken to the .Net Core v5 download page now, please press any key.
    echo:
    pause
)
::Install .Net Core v5
Start "" https://dotnet.microsoft.com/download/dotnet/thank-you/runtime-desktop-5.0.7-windows-x64-installer
:SkipDotNet

However I have been having issues where by it doesn't run correctly on some users systems.

When running the following line of code in cmd by itself (on the users system)

dotnet --list-runtimes | find /i "Microsoft.WindowsDesktop.App 5" | find /i /V "-rc"

Their cmd output is the following:

Microsoft Windows [Version 10.0.19042.1052] (c) Microsoft Corporation. Med enerett.

C:\Users[user]>dotnet --list-runtimes | find /i "Microsoft.WindowsDesktop.App 5" | find /i /V "-rc" 'dotnet' is not recognized as an internal or external command, operable program or batch file.

Is this caused by my code? If so how do I fix it? Or is this an error on their end?

The code works fine on my system but I have .net already installed.

Many thanks for aid in advance.

PS: I know this can also be done in powershell but I require it to be done via batch file.

jps
  • 15,760
  • 14
  • 59
  • 71
Peter
  • 43
  • 1
  • 1
  • 3
  • 1
    `dotnet` is not an internal command, and is only an external command if it is run when the current working directory contains an executable file with the name `dotnet`, and with an extension listed within `%PATHEXT%`; or if a file named `dotnet`, with an extension listed within `%PATHEXT%`, is located within any directory listed within one of the file paths under `%PATH%`. Your code assumes many things, and none of those can be guaranteed to be true for all systems. The best way to determine if your .NET program is installed is to verify that InstallLocation via the Windows registry. – Compo Jul 06 '21 at 20:41
  • Hi, I was following the guidance found here (https://docs.microsoft.com/en-us/dotnet/core/install/how-to-detect-installed-versions?pivots=os-windows). There is no mention of a registry key for v5? Even on stack it was mentioned to use the command I have: https://stackoverflow.com/questions/38567353/how-to-determine-if-net-core-is-installed I can find the keys for v4 using the following article: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#detect-net-framework-45-and-later-versions – Peter Jul 07 '21 at 09:42
  • All of that information is for .NET Framework, not for .NET 'Core'. What I was thinking of was checking through the tree of `HKEY_LOCAL_MACHINE\SOFTWARE\dotnet\Setup`, although [officially](https://docs.microsoft.com/en-us/dotnet/core/install/how-to-detect-installed-versions?pivots=os-windows) Microsoft says that you may need to resort to checking for install folders directly. BTW, i have some memory of having read that the `dotnet` command executable may not be available if the runtime was installed via install script, so do not rely on simply looking for `dotnet.exe`. – Compo Jul 07 '21 at 11:42
  • I fixed it by adding the following before the 1st "dotnet --list-runtimes": WHERE dotnet >nul 2>&1 IF %ERRORLEVEL% NEQ 0 ( Goto :InstallDotNet ) :: Send dotnet results to output log file and find relevant core version dotnet --list-runtimes | find /i "Microsoft.WindowsDesktop.App 5" | find /i /V "-rc" >nul 2>&1 :: If search result is positive then ... IF %ErrorLevel% EQU 0 ( ) else ( DEL /F /Q %TEMP%\MS2netcorecheck.log @dotnet --list-runtimes > "!OutputFilename!" Goto :InstallDotNet ) ::Install .Net Core v5 :InstallDotNet pause Start "" :SkipDotNet – Peter Jul 08 '21 at 21:23
  • Had you read my previous comments, you should have noted that I told you that searching for `dotnet.exe` was not a robust solution to your problem. You could certainly do that, but not in the manner you have. `Where.exe` with `dotnet` will search the current working directory, and the locations within `%PATH%` for all files named `dotnet` which have an extension listed within `%PATHEXT%`. There is no reason to assume that `.EXE` is included within `%PATHEXT%`, _(although it should be)_, but there's certainly no reason to expect that the location of `dotnet.exe` will have been added to `%PATH%` – Compo Jul 08 '21 at 21:46

0 Answers0