The using of visual studio compiler by cmd.exe + batch file - c

Could you help me please with the using of visual studio compiler by cmd.exe + batch file?
I need to create batch file that will be executed in cmd.exe (C:\WINDOWS\system32\cmd.exe).
The body of .bat-script is:
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2017.lnk"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe" D:\temp\ctemplate\2\helloexample.c
Means I want to prepare command line with visual studio environment and then I want to compile file helloexample.c.
The shrtcut/link contains the target: %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
During the execution I see that only first line is handled.
The body of c-script is:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}

During the execution I see that only first line is handled.
If you want to run a batch file from another batch file you have to call it. Unlike an executable code run from a batch file, if you just run another batch file, the system does not return control to the calling batch file. That is why only first line is handled.
So add call to the first line of your batch file:
call "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2017.lnk"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe" D:\temp\ctemplate\2\helloexample.c

Related

How to run batch file inside Visual Studio 2022 Developer Command Prompt

I recently upgraded to VS2022 I cannot find a way to start developer command prompt and run a batch file inside it. Previously I called vsvars32.bat and it set all variables and paths, allowing me to continue in my .bat file. In the current version it does not work and when I call VsDevCmd.bat, the rest of commands in my batch file are not executed.
Is there a way to call developer command prompt or set paths and variables from batch file and continue?
I did not find any other solution so ended up using pipe like this:
type cmds.txt | "c:\Program Files\Microsoft Visual
And cmds.txt contains batch file invocation:
buildall.bat
Not pretty, but works.
I just want to thank you and confirm that this was the only way to automate compilation of a C++ project trhough VS Code.
I tried setting up tasks.json in VS Code and multiple other setups but after 4 days of that nothing worked except your solution PiotrK.
So I ended up preparing a TXT file with a list of commands like so:
set PATH=%PATH%;"C:\Users\DEKOLEV\AppData\Local\Programs\Microsoft VS Code\bin"
cd "C:\path\to\some\folder\with\C++\source\code\files"
code .
}
and I piped the text file directly to the Developer Console link. So now I have a batch file containing the following
type "C:\Some\path\to\file\commands.txt" | "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2022.lnk"
Starting the batch file starts the x64 developer command prompt and runs VS Code from it which enables VS Code to compile the source C++ files with the environment x64 Native Tools Command Prompt for VS 2022 provides.
I don't have enough points to post a comment so I am posting this feedback as an answer. :)

Create Batch file to run mstest for coded ui test automation

Can someone help me out in creating Batch file to run the coded ui test using mstest?
I am currently running the coded ui test in the command prompt by running as administrator by using below lines in command prompt:
cd\
cd C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE>MSTest.exe /testcontainer:D:\Working\LOPS_Testing\LOPS6Automation\bin\LOPS6Automation.dll /test:Light_Oil_Price_System.Scripts.PricingScreen_Script.PricingScreenFunctionality
This works correctly.
But now I want to use the same in a batch file. Can some one help me out to create a batch file by using the above code?
Wrap paths with spaces between quotes.
I also used cd /D, that changes drive if necessary (for example if you were on drive D: you would need to change to C: before cd commands. With /D option this is done automatically)
Also, since you are already on the Common7\IDE folder, you don't need to write again the directory, you could just call MSTest.exe:
cd /D "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE"
MSTest.exe /testcontainer:D:\Working\LOPS_Testing\LOPS6Automation\bin\LOPS6Automation.dll /test:Light_Oil_Price_System.Scripts.PricingScreen_Script.PricingScreenFunctionality

How to create a Simple Build Script for Visual Studio from the Command Line?

I have a lot of Visual Studio Project Solutions in multiple directories (all with the extension .sln) and I want to write a simple batch script that will automatically build all solutions listed in the batch file.
I am able to manually build a solution by starting up the Visual Studio Command Prompt (which is just a command-line instance with the following command executed
"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
After which I then build the project by calling:
devenv "path\to\solutionFile\projectSolution1.sln" /build Debug
This will build the project (assuming the project does not have errors) and I rinse and repeat for each project I want built.
However when I have the following in a batch file called build.bat:
"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
echo "Starting Build for all Projects with proposed changes"
echo .
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug
echo "All builds completed."
pause
The batch script only executes the first line, and waits until I type in exit before executing the others. My understanding of this based on the research I have done on batch files and all the StackOverflow questions is that cmd actually invokes another instance of itself that executes vcvarsall.bat to set up the build environment.
This will not work as typing exit kills that instance with devenv set up and the commands after that cannot execute as devenv is not a recognized command (since the exported path will not exist anymore)
In short, how can this be achieved (passing in the rest of the commands to the cmd instance with devenv defined) in a single batch file? I understand this isn't a robust way (and there are a lot of tools that do this) of invoking builds but I'm just hoping to have one batch script to automate the manual work of individually calling these projects.
Found the solution, as noted by Jimmy, one needs to remove the environment variable %comspec% as that is a shortcut to CMD.exe.
However, just removing "%comspec" /k will cause a CMD instance to open and then exit after a second. I also previously tried the call function which created a separate CMD instance when used with %comspec%
The solution is to add call in front of the first line, and remove %comspec
Here is the final batch file that got things working as intended.
#echo OFF
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
echo "Starting Build for all Projects with proposed changes"
echo .
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug
echo .
echo "All builds completed."
pause
Note that #echo OFF tells the batch script to not echo out the commands (such as that call command) into the terminal (errors and warnings will still be shown, however)
If this is already in a batch script, then this line:
"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
Probably should just be "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
Why? %comspec% is just an environment variable shortcut to cmd.exe, so as you saw, it's launching a new instance of cmd with the /k option specified (which if you run cmd /? says Carries out the command specified by string but remains). You don't care about it remaining, you don't even want a new cmd.exe when you're already running your batch file.

Direct the command to a newly opened separate command prompt using bat file

I have a command prompt shortcut that run a bat file when it's launched. Once it is launched I will issue another command,say my_command, which is added as doskey macro in login.bat
The flow is somewhat like this.
I have a cmd shortcut with target set as:
C:\Windows\System32\cmd.exe /k "D:\\login.bat"
where login.bat sets the environment.
Once the cmd prompt is active I should issue another command, say my_command (which should be run in the currently open cmd prompt)
Inside login.bat I have the following lines
...
doskey my_command=another_login.bat DEBUG 32
I was trying to write a bat file to do the whole thing.
I am not supposed to change login.bat
What I tried is :
start C:\Windows\System32\cmd.exe /k "D:\\login.bat"
my_command
The command my_command gets run on the bat file's command prompt.
How do I make the command my_commnad run on the newly opened command prompt and not in the bat file's cmd prompt?
Try this:
start C:\Windows\System32\cmd.exe /k "D:\\login.bat & devenv"
Answering your edit: bad news.
You cannot run a Doskey macro from a batch file.
Reference: http://ss64.com/nt/doskey.html
Can you just directly run the command you are setting up with doskey?
start C:\Windows\System32\cmd.exe /k "D:\\login.bat & another_login.bat DEBUG 32"
You need to specify the full path to devenv inside the batch file, and that path will depend on your version of Visual Studio.
Try replacing devenv with one of these:
VS 2013:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe
VS 2012:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe
VS 2010:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe
Edit in response to your comment
The doskey.exe executable is located in C:\Windows\System32\doskey.exe. Referencing it by full path should work.

How to run Coded UI test file from Visual Studio command prompt using batch file?

#echo off
#setlocal enableextensions
#cd /d "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE"
start %comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat""
MSTest /testcontainer:C:\testdir\test.dll
Code shown above runs vs command prompt and changes directory to "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE" where MSTest.exe is. But last line doesn't run in vs command prompt window, opens new window and tries to run in a new opened window. Can anyone help how to run ui test file in opened vs command prompt using batch file?
I run my Coded UI tests with the following batch script:
#echo off
:: Running tests without VS Enterprise is possible if you install the Test Agent package: https://msdn.microsoft.com/en-us/library/dd648127.aspx
set test_runner="C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
set test_dlls="C:\Location\Compiled\Tests\Project.CodedUI.Test.dll"
:: If tests is set then only these comma separate test cases are run
:: set tests="Test1,Test2"
set tests=""
if %tests% == "" (
%test_runner% %test_dlls% > CodedUITestResults.txt
) else (
%test_runner% %test_dlls% /tests:%tests%
)
pause
The visual studio number should be replaced per different version
VS2015: 14.0
VS2013: 12.0
Etc

Resources