bat script only runs first line? - windows

When I copy/paste the lines below into a cmd window it executes without a problem.
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
msbuild proj\projsln /p:Configuration=Debug
proj\proj\bin\Debug\proj.exe my args
However when I save it as DoStuff.bat I get the message below (which is the text from executing vcvars32.bat), then nothing else. It does not build my project and obviously doesn't run the newly built executable.
Why doesn't it and how do I have it run all three commands?
>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
Setting environment for using Microsoft Visual Studio 2010 x86 tools.

Use CALL to call another batch file.

Well, there has to be a reason it isn't continuing. Is it that the command is waiting for some input? Thats all that I can think of. Try re-directing the output of the batch file to a log and see what is going on.
Alternatively, split the batch file into separate batch files and put a CALL before each call to the batch file.

Related

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:
Opens a cmd console as administrador
Go to the path of my project/solution (using the CD.., CD commands)
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!
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.
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
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.
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)

Making a script to open, configure, and run multiple programs sequentially

While I have experience coding in Java and c++, I have never scripted anything and would like to learn. I want to create a script or a program which:
Opens Avast Virus Scanner
-deep scans
-quits upon being completed
Opens Malwarebytes
-same as above
opens Spybot Search & Destroy
-same as above
Shuts down computer
I honestly don't know where to even start - should I use a batch file or something else? I would appreciate any help. Thanks
This is only a partial answer, but in order to open programs, you can write a batch file. I found this guide: http://www.online-tech-tips.com/computer-tips/create-windows-batch-files/
#echo off
start "Chrome" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
start "Notepad" "C:\Program Files (x86)\Notepad++\notepad++.exe"
start "VS2012" "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"
start "Outlook" "C:\Program Files (x86)\Microsoft Office\Office14\outlook.exe"
Technically you don't have to touch coding. This can be done through "scheduling" or through some free software. Is it completely necessary to do the coding yourself?

Why does quote location matter in running devenv.com from the windows command line?

I've been putting together a batch script (windows command line), and noticed some strange behavior with quoting paths containing spaces.
To reference locations with spaces (eg c:\Program Files), quotes must be used ("c:\Program Files"). For example, calling MSTest.exe, you would use:
"c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"
If you were going to also reference other programs in that location, you might do something like:
set VSDIR="c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE"
%VSDIR%\MSTest.exe
The expanded call to MSTest.exe would look like this:
"c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE"\MSTest.exe
This works as expected. The quotes are stripped, and the system loads MSTest.exe. Great. Now, if I do the same thing for devenv.com:
set VSDIR="c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE"
%VSDIR%\devenv.com
The system silently does nothing. No output is sent to standard out or error, and the error level remains at zero. Moving the quote after devenv.com loads the process as normal.
Anyone know why I'd see this behavior? Is it because devenv.com runs in real mode, or is it something about the process itself?

Visual studio .SLN executing Exe file / .VBS script

How to launch .Exe file or .VBS script when visual studio solution(.sln) is opened ?
Associate a .SLN file with your own executable instead of devenv. In your executable, if the solution being opened matches then delete the necessary files. Then execute the devenv and pass the solution full path and name as a parameter.

Calling batch files with make and making changes persistent

I'm programming with Visual C++ Express on the command line using makefiles (GNU Make).
For this to work, I have to call the Visual Studio batch file vsvars32.bat to set up the environment. This has to be done everytime I open a new cmd.exe, before using make.
When I try to call the batch file from my makefile, it obviously executes the batch file as
an own process, because the environment is the same afterwards.
So my question: is there a way to execute scripts in cmd.exe like the built-in source command of the Linux/Unix bash? Apart from installing bash on Windows, of course.
Edit after posting my own answer:
The above question is not quite right, it should be like this:
Is it possible to call an environment-changing batch file from within a makefile, so that the changed environment persists for the other programs called in the makefile?
The answer to the original question is yes: you can use the built-in call command of cmd.exe. But since call is a built-in command and not a real program, it doesn't work in a makefile, only if you call a batch file from another batch file.
Answer compiled from the previous answers:
I made a batch file called make.bat which contains the following:
call "%VS90COMNTOOLS%vsvars32.bat"
call make.exe %*
This does the job.
But calling an environment-changing batch file from within a makefile, so that the changed environment persists for the other programs called in the makefile, seems to be impossible.
Edit: After overflowing my PATH variable by repeatedly calling vsvars32.bat, I made the following changes:
if not "%VISUALCVARS%" == "TRUE" (
set VISUALCVARS=TRUE
call "%VS90COMNTOOLS%vsvars32.bat"
)
call make.exe %*
use 'Call':
#echo off
pushd.
call "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars3235.bat"
msbuild LinqSupportClassesSDKBuild.csproj /t:rebuild /p:Configuration=Release /nologo /v:q /clp:ErrorsOnly;
popd
this is the cmd file we use to build our linq provider.
At least in my install of Visual Studio (albeit somewhat ancient VS .NET 2003), one of the links in the VS start menu group is to open a cmd.exe instance with the environment already setup. You might find these helpful:
How to Add Visual Studio Command Prompt (VSCP) to your IDE as a tool?
Running the command prompt from visual studio tools menu
Shortcut: Launch Visual Studio Command Prompt from Visual Studio
They are more geared toward launching the command prompt from the IDE, but they do include information on launching it with the appropriate environment as well which you may find helpful for your purposes.
How do you launch your console? If you are just launching 'cmd' then instead, create a shortcut that executes (%comspec% resolves to c:\windows\cmd.exe or whatever is relevent on your system)
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" x86
Obviously, change the path to point to the proper installation folder.
More generally, as the above poster pointed out, if a .cmd file needs to process another .cmd file rather than launch it as a seperate process, use the 'call' batch command.
Wrap GNU make in a script (mmake.bat). Put the script in the path somewhere.
The script itself should run the vsvars32.bat and then make, like this
vsvars32.bat
make %*
As far as I remember, invoking a script from another script like this is done within the same shell (similar to Bash "." command).
I have found three solutions to this problem:
1) If the environment variables being set by the batch file are static (that is, they are always the same values), set those values for your entire user profile. Right-click on My Computer, click Properties-->Advanced-->Environment Variables. Add the variables from the batch file to the User Variables or System Variables section (User variables are only visible by you, System variables are visible by all users of that computer).
2) Write a wrapper batch file that calls the environment setup script then calls the Makefile.
3) Instead of using the SET command to set environment variables in the batch file, use the SETX command (requires the Windows Resource Kit). SETX is similar to SET, except it makes its changes to the master environment in the registry and will take effect in all command prompts launched in the future (but not the current one).

Resources