-1

I trying to do a batch script to found in my system the MSBuild.exe to build my project using the command line.

I found the paths using the command:

where /R "C:\Program Files\Microsoft Visual Studio" MSBuild.exe

This command return a list of paths:

C:\>where /R "C:\Program Files\Microsoft Visual Studio" MSBuild.exe

C:\Program Files\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe
C:\Program Files\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\amd64\MSBuild.exe
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\MSBuild.exe

I need to get these list of paths, set a variable and run a command line to get the version of the file founded.

I tried:

for %%G in (where /R "C:\Program Files\Microsoft Visual Studio" MSBuild.exe) do (
    set TEMP_MSBUILDER %%~G
    %TEMP_MSBUILDER% --version
)

I think that my problem is in the for loop. Have another way to find the file on system and set the founded path on variable?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Arcaniaco
  • 400
  • 2
  • 10
  • 1
    If you open a Command Prompt window, type `for /?` and press the `[ENTER]` key, you will see that `command`s are placed within the parentheses between either, single quotes, ```('where /R "C:\Program Files\Microsoft Visual Studio" MSBuild.exe')```, _(or if using `usebackq`)_ between back quotes, ```(`where …`)```. Please, in future, before wasting peoples time with questions, read the usage information for your commands, and use the built in search facility, _(as there are literally thousands of examples showing this syntax)_. – Compo May 26 '22 at 13:59
  • Example for usage in a batch file: `for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe /R "C:\Program Files\Microsoft Visual Studio" MSBuild.exe 2^>nul') do echo %%I& "%%I" --version` After executing `for /F` in a command prompt window and reading the entire output help carefully and completely from top of first to bottom of last page as suggested already by Compo, run `set /?` and read also the output usage help completely, especially the paragraphs about delayed expansion of environment and dynamic variables. If it is easy possible like here, avoid environment variables in FOR loops. – Mofi May 26 '22 at 14:35
  • Another method is `for /F "delims=" %%I in ('dir "C:\Program Files\Microsoft Visual Studio\MSBuild.exe" /A-D /B /S 2^>nul') do echo %%I& "%%I"` which outputs the same information as the command line in my previous comment. The difference is using internal command __DIR__ of `cmd.exe` instead of external command __WHERE__. The __DIR__ solution is faster although you will most likely not notice the execution time difference. – Mofi May 26 '22 at 14:39

1 Answers1

0

Asking the program "What version are you?" with the --version switch has problems and probably should be avoided. On my system the arm64 version throws up a cmd.exe - Machine Type Mismatch box complaining:

The image file C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\arm64\MSBuild.exe is valid, but is for a machine type other than the current machine.

And spits out the following error text that can be piped to null:

This version of C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\arm64\MSBuild.exe is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher.

Since the error box seems unavoidable, I beleve this answer is probably a better solution, and the following code is based on it.

@ECHO OFF
    SetLocal EnableExtensions EnableDelayedExpansion
    FOR /F "USEBACKQ EOL=~ TOKENS=* DELIMS=~" %%G IN (`where /R "C:\Program Files\Microsoft Visual Studio" MSBuild.exe`) DO (
        SET "File=%%~G"
        SET "File=!File:\=\\!"
        FOR /F "USEBACKQ EOL=~ TOKENS=* DELIMS=~" %%S IN (`WMIC DATAFILE WHERE name^="!File!" get Version /format:Textvaluelist`) DO (
            FOR /F "DELIMS=" %%C IN ("%%S") DO SET "%%S"
        )
        ECHO;%%~G=!Version!
    )
Darin
  • 1,423
  • 1
  • 10
  • 12