How to build solution using batchfile

22,341

Solution 1

The visual studio command prompt just loads some variables and path settings. That's all it is, it's nothing specially, it's not a different command prompt, it's the same command prompt with some settings configured. You can load the same settings in your own batch file by including the following line at the top:

call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86

(Obviously, for different versions of VS, the path might change slightly)

You can replace "x86" with the appropriate architecture for your machine. The permitted values are:

  • x86
  • amd64
  • x64
  • ia64
  • x86_amd64
  • x86_ia64

That said, I don't think you actually need to load all the vars/paths all you really need to do is provide the full path to the devenv.exe file. You could try this instead:

"c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" /build release "D:\Source Code\Source\test.sln"

(Again, the path will change for different versions of visual studio)

Solution 2

The sample batch file below will detect the install directory that contains devenv.exe by looking it up in the registry (for VS2005, can easily be adapted for other versions) and executes devenv.exe. Is this what you're looking for?

@echo off

CALL :GETVS2005DIR
IF "%VS2005DIR%" == "" GOTO NOVS2005
IF NOT EXIST "%VS2005DIR%" GOTO NOVS2005

%VS2005DIR%devenv.exe ...
GOTO :EOF

:GETVS2005DIR
for /f "tokens=1,2* delims= " %%i in ('reg query HKLM\Software\Microsoft\VisualStudio\8.0 /v InstallDir') do set VS2005DIR=%%k
GOTO :EOF

:NOVS2005
echo.
echo Visual Studio 2005 installation directory not found
echo.
GOTO :EOF

Note also that as long as your solution does not contain a Setup project, you will normally be able to build it using MSBUILD, which is simpler and works on a machine without Visual Studio installed:

REM Check MsBuild is available (this is for V2.0, use a different version if desired)
SET MSBUILD=%WINDIR%\Microsoft.NET\Framework\v2.0.50727\MsBuild.exe
IF NOT EXIST "%MSBUILD%" GOTO NOMSB

"%MSBUILD%" MySolution.sln /t:rebuild /p:configuration=Debug /verbosity:quiet 
GOTO :EOF

:NOMSB
echo. 
echo MSBUILD not found 
echo. 
GOTO :EOF 

Solution 3

Not sure if I understand the question.

Just create a file called test.bat, add the statement you've written above to that file and then just open a VS command prompt and type [pathtobatfile]\test.bat.

Share:
22,341
Ram
Author by

Ram

Updated on July 09, 2022

Comments

  • Ram
    Ram almost 2 years

    I want to build .NET solution using batch file.

    I am aware that I need to use following statement

    devenv /build release "D:\Source Code\Source\test.sln"
    

    But I do not know how to create batch file which will execute on VS command prompt.

  • Ram
    Ram about 14 years
    well I don't want to goto VS command prompt. As it is a batch file, I want it to execute on VS command prompt when I click it.
  • Hans Olsson
    Hans Olsson about 14 years
    Then as Simon says all you need to do is to add a call to the batch file executed by the VS command line shortcut at the top of your batch file and then all you need is to create a shortcut to that batchfile.
  • Ram
    Ram about 14 years
    This will work for me. But when I put it in a bat file and run it, it exited without building the solution. How can I keep the command window open to show the current status of build process?
  • Kildareflare
    Kildareflare almost 13 years
    @Ram Add the Pause command to the end of your batch file and the window will stay open until you press any key.
  • Vahid Ghadiri
    Vahid Ghadiri over 2 years
    @Ram Check release folder. Note that you choose release not debug.