How to create a Batch File for Visual Studio Command Prompt - visual-studio

I want to create a batch file for Visual Studio 2008 x64 Cross Tools Command Prompt to do something continuesly in my PC, here is the senario.
svn update
delete some files
MSBuild MySolutiuon.sln
delete some files
xcopy somefiles
MSBuild AutomateBuildConfiguration.xml /p:Configuration=Release
xcopy some files
delete somefiles
xcopy some files
create a Zip file if it is possible // it is not neccessary
I can do most of it with simple Command Prompt and MSBuild parts with Visual Studio Command Prompt, but as these two prompt are different I cannot complete my senario.
I have tested all command and work great for me, Give me a solution if you know what should I do.
I checked this and didn't underestand anything
Thank you in advance

Make the first line of your batch file set up the VS environment:
call "C:\Program Files\Microsoft Visual Studio 2008\VC\vcvarsall.bat" x86_amd64
svn update
delete some files
MSBuild MySolutiuon.sln
... more commands ...
x86_amd64 is the argument used for x64 Cross Tools Command Prompt.
Once vcvarsall.bat has run, msbuild will be available in the path for the rest of the commands in your batch file.
Alternatively, if you aren't using Visual C++, you might prefer to set up the environment with this line (instead of the call to vcvarsall.bat):
For VS 2008:
call "%vs90comntools%vsvars32.bat"
For VS 2010:
call "%vs100comntools%vsvars32.bat"
For VS 2012:
call "%vs110comntools%vsvars32.bat"
For VS 2013:
call "%vs120comntools%vsvars32.bat"
For VS 2015:
call "%vs140comntools%vsvars32.bat"
For VS 2017:
Batch is now called vc not vs.
call "%vs140comntools%\..\..\VC\Auxiliary\Build\vcvars32.bat"
or better
call "%vs140comntools%\VsDevCmd.bat"

For Visual Studio 2015:
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64
For Visual Studio 2013:
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86_amd64

For Visual Studio 2010, this is working great:
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86

For Visual Studio 2019 :
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64

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

I wrote a bat file using the following steps and it worked.
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86_amd64
echo call complete
pause
cd C:\tfs.sbdinc.com
tf get $/MAC_MBA/CoreApplicationAndReports/Main/Application/Solution /recursive
echo get complete
pause
cd C:\tfs\CoreApplicationAndReports\Main\Application\Solution
msbuild
echo build complete
pause
devenv mba.sln
echo ide launch complete
pause

Try below batch file to run the MS test/Nunit test for C# tests.
#echo off
echo Run the CMD(Command Prompt) Program.
echo.
pause
cls
echo Target Framwork
CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319
echo 'Project Path'
msbuild "C:\Git\ElementsCloud.Tests\Source\ElementsCloud.Tests.sln"/p:configuration=debug
pause
echo 'Project Dll path' and Execute tests
cd C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TestWindow
vstest.console.exe "C:\Git\ElementsCloud.Tests\Source\Selenium.Tests\bin\Debug\net471\Selenium.Tests.dll" /Tests:Test1,Test2,Test3
echo.
PAUSE
EXIT

Related

Visual Studio: missing Shortcut: "Visual Studio Command Prompt"

Because the VS 2015 Installer was crashing all the time, I had to use the silent installation. Now I don't have the "Visual Studio Command Prompt" Shortcut in Start menu. Can I find it somewhere else?
I had to create the shortcuts manually as well, but they all follow the same pattern so no biggie:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" x86
Don't forget to change the execution path to: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\"
Replace the last bit (x86) with the tool you want to use:
x86
amd64
arm
x86_arm
x86_amd64
amd64_x86
amd64_arm
Visual studio command prompt is nothing but the regular command prompt where few environment variables are set by default. This variables are set in the batch script : C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat . So basically to get a visual studio command prompt for a particular version, just open regular command prompt and run this batch script : C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat (Change the visual studio version based on your installed version). Voila you have got the visual studio command prompt. You can write a script to run the batch file and open cmd.exe and have a shortcut for the same.

vcvarsall - Using 64bit compiler and then switching back to 32bit for seperate build

I'm using vcvarsall to switch to x64 compile tools for VS2010, as I run into memory issues with a certain build. However I'd like to also switch BACK to x86 tools for regular builds.
Currently I have a batch file that looks like this:
CALL "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x64
set _IsNativeEnvironment=true
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv" "C:\Development\projectx.sln" /build "Debug|x64"
CALL "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
set _IsNativeEnvironment=true
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv" "C:\Development\projectx.sln" /build "Debug|x86"
This works for the first build, but the second will still launch 64bit compiler/linker - which gives errors occasionally (why I need to use 32 for it).
In testing I found that it will work only if I open a new command line after running the x86 vcvarsall.bat - how can I mimic this in a batch file?
Use setlocal and endlocal
setlocal
CALL "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x64
set _IsNativeEnvironment=true
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv" "C:\Development\projectx.sln" /build "Debug|x64"
endlocal
setlocal
CALL "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
set _IsNativeEnvironment=true
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv" "C:\Development\projectx.sln" /build "Debug|x86"
endlocal

Jenkins Visual Studio x64 prompt (for CMake and Ninja)

I have a Jenkins slave with Visual Studio 2012 and want to build for x64. What I need is the prompt environment I get when I run the tools prompt link in the Windows Start Menu. People suggest to do it like this (in a Jenkins Windows Batch prompt):
call "%VS110COMNTOOLS%vsvars32.bat" x86_amd64
But this is not enough. There are small differences in the PATH, LIB and LIBPATH environment variables: the paths in there point to the x32 paths only, e.g. to
...;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\BIN;...
instead of
...;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\BIN\x86_amd64;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\BIN;...
In fact I am trying to build with the Ninja generator from CMake where the build configuration is determined by the prompt environment.
You need to call vcvarsall.bat x86_amd64 which is located in the VC-subdirectory (and eventually remove parentheses from the PATH):
set path=%path:"=%
call "%VS110COMNTOOLS%..\..\VC\vcvarsall.bat" x86_amd64
If you want to run this in a Pipeline script:
bat """set path=%path:\"=%
call "%vs110comntools%..\\..\\VC\\vcvarsall.bat" x86_amd64
..."""

How to run a script at the start of the visual studio command-line tool?

I am using visual studio 2010
I need a script that launches the visual studio console and executes some commands
More specifically something like this:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86
msbuild smothing
cd somewhere
etc...
However after executing the first line and entering the visual studio mode the script stops
How can I make it run msbuild and everything else in one go?
I needed to change the top line with this:
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86

How to get cmd line build command for VS solution?

This is probably easy but I am getting stuck: when I build a solution in Visual Studio - how do extract the exact cmd line for the current build command in order to be able to do the same build from VisualStudio console?
In the output window I can see the single projects in the solution build commands but not the one for the whole solution.
I am on VS2005.
Any help would be appreciated
Navigate to your Programs menu > Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005 Command Prompt.
this command prompt has all the necessary .NET environment variables set for the the command line session. You can change directory to your solution directory (e.g. c:\projects\mySolution) and run
Msbuild.exe mySolution.sln
You can see the various options available using msbuild /?
Msbuild is located at C:\Windows\Microsoft.NET\Framework\v2.0.50727
On top of msbuild /? quick-option check, you may reference the MSBuild Command Line Reference page for more explanations on its usage. And how to build specific targets in solutions.
In addition to what #JohnIdol says correctly, I've found that you need to setup a number VS environment variables. I don't have the name of the batch file in front of me, but you can modify or 'I think' use it. It is in VS program files tree somewhere. Also, as I remember you don't want to be in a standard shell but a .NET setup shell for some paths and such. I'll add details later when I'm at a Windows PC with VS.
EDIT: The batch file mentioned is a shortcut in ProgFiles menu. Here is the details of its properties.
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat""x86"
Here is my batch file, using MSBuild to call the solution.
#echo off
:: setup VS2005 command line build environment
set VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8
set VCINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8\VC
set FrameworkDir=C:\WINDOWS\Microsoft.NET\Framework
set FrameworkVersion=v2.0.50727
set FrameworkSDKDir=C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0
set DevEnvDir=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual Studio 8\Com
mon7\Tools;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\bin;C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\bin;C:\Program Files\Microsoft
Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\VCPackages;%PATH%
set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\INCLUDE;C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;C:\Program Files\Microsoft Visual
Studio 8\VC\PlatformSDK\include;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\include;%INCLUDE%
set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\LIB;C:\Program Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual Studio 8\VC
\PlatformSDK\lib;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;%LIB%
set LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\LIB
echo %0 %*
echo %0 %* >> %MrB-LOG%
cd
if not ""=="%~dp1" pushd %~dp1
cd
if exist %~nx1 (
echo VS2005 build of '%~nx1'.
echo VS2005 build of '%~nx1'. >> %MrB-LOG%
set MrB-BUILDLOG=%MrB-BASE%\%MrB-WORK%.%MrB-NICEDATE%.%MrB-NICETIME%.build-errors.log
msbuild.exe %~nx1 /t:Rebuild /p:Configuration=Release > %MrB-BUILDLOG%
findstr /r /c:"[1-9][0-9]* Error(s)" %MrB-BUILDLOG%
if not errorlevel 1 (
echo ERROR: sending notification email for build errors in '%~nx1'.
echo ERROR: sending notification email for build errors in '%~nx1'. >> %MrB-LOG%
call mrb-email "Mr Build isn't happy about build errors in '%~nx1'" %MrB-BUILDLOG%
) else (
findstr /r /c:"[1-9][0-9]* Warning(s)" %MrB-BUILDLOG%
if not errorlevel 1 (
echo ERROR: sending notification email for build warnings in '%~nx1'.
echo ERROR: sending notification email for build warnings in '%~nx1'. >> %MrB-LOG%
call mrb-email "Mr Build isn't happy about build warnings in '%~nx1'" %MrB-BUILDLOG%
) else (
echo Successful build of '%~nx1'.
echo Successful build of '%~nx1'. >> %MrB-LOG%
)
)
) else (
echo ERROR '%1' doesn't exist.
echo ERROR '%1' doesn't exist. >> %MrB-LOG%
)
popd
For VS .NET 2003 you can use devenv.exe to build the solution/project from command line.
devenv solutionfile.sln /build solutionconfig
E.g. usage in batch file:
call "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat"
devenv Tools.sln /build "Release"
If you want to see the exact command line as provided by VS (rather than work it out) then you could try replacing the MSBuild.exe with your own console app that prints out all the parameters to a file.
You could also write out all the environment variables supplied to check which ones VS provides in the background.
I just want to thank the Bejoy on the example. I had big problems with solution rebuild on setup pre build event because they removed most of the macros and this helped me a lot.
Here is my solution based on Bejoy's (considering that .bat file is located in setup root folder which is part of solution):
call "%VS100COMNTOOLS%\vsvars32.bat"
devenv "%CD%\..\soulutionfile.sln" /build "Release"
You can start msbuild from the command line. msbuild understands .sln (solution) files. You can specify the .sln file and the build configuration (debug, release etc.) from the command line.
http://msdn.microsoft.com/en-us/library/ms164311.aspx
Here is the documentation on what you can do with msbuild. MSBuild.exe is installed with the .net framework, not with visual studio. You can find it in c:\windows\microsoft.net\framework\v3.5 (or v2.0.50727)
I searched a bit and found that you can also do a command line build with visual studio using devenv.exe /build, more info here:
http://msdn.microsoft.com/en-us/library/xee0c8y7(VS.80).aspx

Resources