execute exe from incomplete name in .bat

I wanna make a .bat for installing service, I use that

C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe "ServiceName.exe"

But the "v4.0.30319" can be "v4.0.otherVersion" and I look for a way to execute anyway. I searched a lot of but didn't find something who work and I'm really bad with bash.


1 answer

  • answered 2020-06-02 12:56 Compo

    Based upon your provided information, this idea retrieves the location you require from the registry:

    @Echo Off
    SetLocal EnableExtensions DisableDelayedExpansion
    Set "Net4RegPath=HKLM\SOFTWARE\Microsoft\Net Framework Setup\NDP\v4"
    Set "InstallUtil="
    For /F "EOL=HTokens=2*" %%G In (
        '%__AppDir__%reg.exe Query "%Net4RegPath%" /S /F "InstallPath" /V /E
         2^>NUL^|%__AppDir__%find.exe "_SZ"')Do For /F "Delims=" %%I In (
        '%__AppDir__%where.exe "%%~H\.":"InstallUtil.exe" 2^>NUL'
    )Do Set "InstallUtil=%%I"
    If Defined InstallUtil "%InstallUtil%" "ServiceName.exe"

    Please note that this will not currently retrieve the appropriate file path if you're wanting to specifically identify the location of the x86 InstallUtil.exe file on an x64 OS. But as that is technically outside of the scope of your question at the time of answering, I'll leave that for you to implement if required.