MikePR
MikePR

Reputation: 2986

How to execute 'msbuild' command from a batch file

I would like to create a batch file to make the builds from my VS project in on click. All the days I do the following steps:

  1. Opens a cmd console as administrador
  2. Go to the path of my project/solution (using the CD.., CD commands)
  3. Write the following command to make the build:

    msbuild mySolution.sln /p:configuration=debug

As commented before, I'd like to make all this process in one simply click. So, I'm trying to create a .bat file to do it.

This is the code of my batch file:

set location="C:\myPath\..\MyFolderSolution"
set pathMSBuild = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe"

@echo off
cls

call %pathMSBuild%
cd %location%
msbuild.exe "lucre.sln" /p:configuration=debug
pause

However, when I try to execute the batch file I get the following error: 'msbuild' is not recognized as an internal or external command, operable program or batch file.

Any clue or help to know if it is possible and if so, how to do it will be very appreciate

Regards!

Upvotes: 23

Views: 41725

Answers (4)

johnsabre
johnsabre

Reputation: 353

You can also add the directory which MSBuild.exe is located, to the PATH environment variable so you can call msbuild anywhere.

So in the bat file it can be something like this:

cd C:\PathToSoultion\
msbuild TheSolutionName.sln /p:configuration=debug
pause

I know the question is already years away but I hope this might help someone.

Upvotes: 4

Archeg
Archeg

Reputation: 8462

Both answers use hardcoded paths, which might not always work. Use this instead:

@if exist "%VS100COMNTOOLS%vsvars32.bat" call "%VS100COMNTOOLS%vsvars32.bat"

Change VS100 to any other version, you can also put these lines one by one to support many versions of VS simulteneously:

@if exist "%VS100COMNTOOLS%vsvars32.bat" call "%VS100COMNTOOLS%vsvars32.bat"
@if exist "%VS140COMNTOOLS%vsvars32.bat" call "%VS140COMNTOOLS%vsvars32.bat"

If done this way, you no longer dependant on a specific version of .Net Framework or Visual Studio

Upvotes: 4

stevejoy32
stevejoy32

Reputation: 374

You're not in the right directory, you need to cd to the directory that msbuild is in. Try this:

set pathMSBuild="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\"
@echo off
cls
cd %pathMSBuild%
msbuild.exe "C:\myPath\..\MyFolderSolution\lucre.sln" /p:configuration=debug
pause

Or you could add the msbuild directory to your path, and skip the lines with set and cd.

Upvotes: 22

Vertigo
Vertigo

Reputation: 634

If you don't want to cd to the msbuild installation directory, you can also load the Visual Studio Environment Variables like this:

call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" x86_amd64

(See also this Question)

Upvotes: 2

Related Questions