Setting and using an environment variable in the same command - windows

I am using Windows 7 and I want to set up an environment variable and use it in the same command.
Specifically, I want to execute the following 2 commands simultaneously as a single command-:
set MYPATH="C:\Program Files (x86)\Microsoft Visual Studio 11.0"
%MYPATH%\VC\vcvarsall.bat
In other words, I want the Windows version of this.
This is what I've tried so far -:
set MYPATH="C:\Program Files (x86)\Microsoft Visual Studio 11.0" && cmd.exe /C "%MYPATH%\VC\vcvarsall.bat"
But it isn't working.
So, Is there any way of doing this in Windows ?

This can be done by writing -:
cmd.exe /X /V:ON /C "set MYPATH="C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\varsall.bat" && !MYPATH!"
Source

Related

Visual studio 2017 Developer Command Prompt switches current directory

I am getting weird behavior of "Developer Command Prompt for VS 2017" command line tool. Normally in previous versions of visual studio this script (VsDevCmd.bat) was not messing current directory from where you run it. Now it seems to change it. One simple workflow would be just to start the shortcut "Developer Command Prompt for VS 2017" and it doesn't honor the "Start in" directory:
By any chance anyone seen this issue? It really bugs me because I used to have a shortcut with it and start CMD in my source directory, use TFS/msbuild commands afterwards.
You can set the VSCMD_START_DIR environment variable to have vsdevcmd.bat change to that directory when it finishes. Otherwise it will check if you have a %USERPROFILE%\source directory and change to that (which is what you see).
You can change the "Target" for the "Developer Command Prompt for VS 2017" to something like the following to have change to a particular directory:
%comspec% /k "set VSCMD_START_DIR=C:\temp && "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat""
Note that the path to vsdevcmd.bat needs to be put in an additional set of double quotes.
Alternatively, rename or remove the %USERPROFILE%\source directory (Note that this seems to be some kind of new "standard" directory for sources), that will make vsdevcmd.bat honor the "Start In"-value (i.e. "current directory").
:: Some rocket scientist in Redmond made the VS15 VsDevCmd.bat change the cwd; the pushd/popd fixes that.
pushd %CD%
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat"
popd
"C:\Program Files (x86)\CruiseControl.NET\server\ccnet.exe" %*
vsdevcmd_end.bat, REM out the line:
cd /d "%USERPROFILE%\Source"
#REM Set the current directory that users will be set after the script completes
#REM in the following order:
#REM 1. [VSCMD_START_DIR] will be used if specified in the user environment
#REM 2. [USERPROFILE]\source if it exists
#REM 3. current directory
if "%VSCMD_START_DIR%" NEQ "" (
cd /d "%VSCMD_START_DIR%"
) else (
if EXIST "%USERPROFILE%\Source" (
cd /d "%USERPROFILE%\Source"
)
)
If I see your quastion in right way that I'm used to work with EntityFramework Core with the help of CMD or PowerShell. For this you should right-click In the Solution Explorer panel then in the context menu, click Open Folder in File Explorer. In the address bar of the file explorer type cmd or powershell, you will start the command line from the project folder.

Adding Native Tools Command Prompt on VS2015 RC

Since I cannot locate Native Tools CMD under the Tools menu, I try to manually add it in External Tools. Few questions:
Regardless of what I choose for Command (ARM, x86 or x64 etc.), Command is always C:\Windows\System32\cmd.exe. Why the different CMDs end up having the same path to the native System32's CMD?
Referring to this answer, I should insert /k "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" to the arguments - what is the /k and what is the bat for this argument? Why do I need to pass a path as an argument to the command prompt?
What is Initial Directory?
Why the different CMDs end up having the same path to the native System32's CMD?
The VS2015* CMDs are just cmd.exe with some environment variables already set up for you. so for example instead of typing "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" to run InstallUtil.exe you will just type InstallUtil and it will work. if you didn't set up the environment variables you will get an error message saying that 'installutil' is not recognized as an internal or external command, operable program or batch file.
what is the /k and what is the bat for this argument? Why do I need to pass a path as an argument to the command prompt?
/k is a parameter for cmd.exe and what it does is run the commands specified by the string that follows (in this case it will execute what's inside "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat" and will carry out).
What is Initial Directory?
Initial directory is used to specify the working directory that your cmd.exe instance will start in
So in the end you'll have something like this for Visual Studio 2015:
The "arguments" for VS2015 is :
/k "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
I took a look at my start menu and right clicked on Developer Command Prompt for VS2015. Copied target %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat""
MSBuild Command Prompt for VS2015
Copied target %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsMSBuildCmd.bat""
https://connect.microsoft.com/VisualStudio/feedback/details/747807/vs2012-x64-native-tools-command-prompt

Execute batch file. How to call .bat file, visual studio command prompt and change directory in opened command prompt window

#echo off
echo copy masterDB file from one directory to another one
copy "C:\dir\dbfile" "C:\dir1\dbfile"
cd c:\lvsdir
call lvsrun.bat
timeout /t 180
start %comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86
cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE
MSTest /testcontainer: C:\testdir\test.dll
I want to do via a batch file to copy a db file from one directory to another one(which executes correct), then should start lvsrun.bat file, which should start lvs server,
and then to open visual studio command prompt in a new window, change directory in opened command prompt to test directory and run test file. Problem occurs when i call lvsrun.bat, and it stucks there. New vs command prompt can't be opened. And having problem with changing directory in opened vs command prompt and run test file. Code above doesn't really work
You've asked two questions here. You should split them up and ask them as two separate SO questions.
Q1. Why is my batch file never getting past call lvsrun.bat?
A1. Because call will not return until the batch file it is calling has exited. If you want to launch lvsrun.bat and continue execution immediately, use start.
copy "C:\dir\dbfile" "C:\dir1\dbfile"
cd c:\lvsdir
start "" "%comspec%" /k lvsrun.bat
Q2. Why doesn't the new command window I launch run my test file?
A2. Your batch file will only control its command window. If you launch another command window, that one is on its own, you can't "send" commands to it. But you could instead run the test in the current window rather than launching another:
:: Use "call" here to run vcvarsall.bat to set up the environment in this process
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE
MSTest /testcontainer: C:\testdir\test.dll
Or you could make a second batch file just for running the test. For example, let's call it runtest.bat, and give it those exact same lines:
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE
MSTest /testcontainer: C:\testdir\test.dll
which would then get called from your original batch file either synchronously:
call runtest.bat
or asynchronously:
start "" "%comspec%" /c runtest.bat

Creating batch file to open Visual Studio command prompt?

I have tried something like this but did not work:
#echo off
call "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"
mstest /testcontainer: mytest.dll
save as .bat file and when i double click it does nothing.
So, I am trying to open command prompt located in visual studio and execute.
Are you trying to change the directory to "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"?
Try this:
#echo off
CD "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"
mstest /testcontainer: mytest.dll
PAUSE
Your batch file seems to be a little out of shape there. I'm no expert, but you have:
call "C:\Program Files\Microsoft Visual Studio 2008\VC\"
...but without a batch file name. See this for info.
If mstest is your batch file name then the call should be more like:
call "C:\Program Files\Microsoft Visual Studio 2008\VC\mstest.bat " <arguments>

Running a Windows Batch Script to Startup Multiple Files

I'm trying to replace the programs that run from my startup directory with a batch script. The batch script will simply warn me that the programs are going to run and I can either continue running the script or stop it.
Here's the script as I have written so far:
#echo off
echo You are about to run startup programs!
pause
::load outlook
cmd /k "C:\Program Files\Microsoft Office\Office12\OUTLOOK.EXE" /recycle
::load Visual Studio 2008
call "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"
Both of these commands will load the first program and wait until I close it to load the second. I want the script to load the processes simultaneously. How do I accomplish this?
Edit: When I use the start command it opens up a new shell with the string that I typed in as the title. The edited script looks like this:
start "C:\Program Files\Microsoft Office\Office12\OUTLOOK.EXE"
::load Visual Studio 2008
start "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"
This works:
#echo off
echo You are about to run startup programs!
pause
::load outlook
start /b "" "C:\Program Files\Microsoft Office\Office12\OUTLOOK.EXE" /recycle
::load Visual Studio 2008
start /b "" "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"
Use START like this:
START "" "C:\Program Files\Microsoft Office\Office12\OUTLOOK.EXE"
When your path is enclosed in quotes, START interprets it as the title for the window. Adding the "" makes it see your path as the program to run.
There is the start command that will behave much like if you clicked the files in Explorer.

Resources