Run Batch File in VS2008 - visual-studio

Is it possible to run a batch file as a menu item in studio 2008?

Yes, go to Tools | External Tools in the menus and add your batch file as the Command. You can also pass in any arguments that you want such as the build target, project directories, etc.
Once you do this, your batch file will appear as one of the items on your tools menu. If you want, you can then add a toolbar button for it by going Tools | Customize | Tools and dragging the External Command # that matches your tool to the toolbar. You will then want to change it to an icon and select an image for it.
The tools are numbered 1 based in the order they appear in the menu.

Run batch files in VS Solution Explorer directly
This is much better than having external tool since you can execute a batch file simply by double clicking on it in solution explorer just as if you were opening a code file. I've written a blog post about it that will help you lots to configure it and of course use it.
A sidenote
You've asked this question 2 years ago, but I see that nobody mentioned this technique so I'm providing this answer for any future reference. I hope it gets upvoted, so it will be near the top for readers to see.

Use "Tools"/"External Tools..." menu item and add your bat-file to "Tools" menu

As A refinement to the given answer:
Say your project/solution have several BAT that are project specific - you wouldn't want to run one from another project - that's what could happen with the above answer - because you have hard wired the path to the bat.
Just set up one external tool nameed "Run BAT", sets its Command to $(ItemPath), and set the initial directory to $(ItemDirectory), The dialog will whinge that this is not a real executable - but ignore that.
Tie the tool to a button if you want.
Now to run the BAT of choice from the project or solution - just open the .bat for editing and select the "Run BAT" external tool.
you could also set the "use output" option as well and the bats output will be in the output window.

Related

How can I manipulate MKS sandboxes and members through Visual Studio?

As with most other versioning systems some brilliant individual, or group thereof, has established an extention to Visual Studio so that a developer may manage the state of their files directly with the application they work most often in.
Sadly, MKS doesn't really have that kind of Open-Source following, nor does the user base of the application weigh heavily with the developers out there.
I'm kind of setting myself up for an answer here, but it was a refreshing note here in the office when my coworker opened this can of worms...turned out many of our developers already had some of these in their personal toolboxes...these are some of the commands we came up with.
To start, there is a rather easy way to add custom tools to the menus in Visual Studio :
Tools --> External Tools
This dialog is pretty much a wizard to set up quick command line options in the application that you can later refer to when adding a menu option to a tool bar or flyout.
Note that the order of the list of Menu Contents will be crucial in figuring out which item in Visual Studio to add to your menu/toolbar. For instance ::
When you access your Tool Customization dialog, the Command will be listed under Category 'Tools' and will be enumerated with the following format :: 'External Command XX' where XX is the number corresponding to the position of your command in the list.
(eg MKS-AddMember would be 'External Command 3')
OK...now that that's behind us, let me list out a few different MKS commands that translated well for us. These will act as the Arguments in the External Tools dialog...
These can all be run from your command line prompt. The executable is (usually) at C:\Program Files (x86)\Integrity\IntegrityClient10\bin\si.exe
Checkout Member :: co -g --sandbox="$(ItemDir)\project.pj" "$(ItemPath)"
Add Member :: add -g --createSubprojects --sandbox="$(ItemDir)\project.pj" "$(ItemPath)"
View History :: viewhistory -g --sandbox=”$(ItemDir)\project.pj” “$(ItemPath)”
Diff Two Files :: difffiles -g “$(ItemPath)”
Revert :: revert -g --sandbox="$(ItemDir)\project.pj" "$(ItemPath)"
You can also get creative and find other commands by running “si -?” from a command line.
Post more findings here of things you find useful!
As a follow on to beauXjames' answer, I found it useful to add these commands to the context menu in the Solution Explorer:
Tools->Customize->Commands tab->Context menu:“Project and Solution Context Menus | Item”
Add New Menu (i.e. MKS)
Reselect that new menu from the drop down
Add your ExternalCommand1,2,3… to that

I want to redirect output from our code review tool that we run in VS2010, so that we can click on output to goto file, line, column

I want to redirect output from our code review tool that we are writing in VS2010, so that it's messages are parsed by Vs2010 and we can click on them to goto file, line, column.
I vaguely remember learing about this in vs2005, you output your text to a certain kind of window, with the exact format below, and then vs would parse the message and you could click on it to goto the specific location...
D:\Project Files\CIS3G\Webapp_Test_BLL\Evaluation\Reports\TestEvaluationHistoryBLL.cs(27,44): warning CS0649: Error text
It was constructed thusly :
full file path:[error or warning type]:error message
Then it just worked "magically"..
But this is all I can remember from a conversation ~10 years ago about how to do this.
Does anyone remember what I'm talking about and can direct me to information about how to make this work with output from a tool we write to work within visual studio 2010?
Maybe a tutorial?
This post:
Formatting the output of a custom tool so I can double click an error in Visual Studio and the file opens
Talks about doing it in the build, but I'd like to be able to run the code review tool independantly and get it to work too...
You've pretty much got it right in your question.
As an example, create a file in your temp directory (in a command prompt do echo %temp% to find it) called test.bat
Add the following to test.bat:
#echo D:\Project Files\CIS3G\Webapp_Test_BLL\Evaluation\Reports\TestEvaluationHistoryBLL.cs(27,44): warning CS0649: Error text
Now in VS10, select Tools->External Tools.... In the new window, select Add.
In the Command: field, enter %temp%\test.bat
Select Use Output window and click OK.
Your new tool should now show up in VS10's Tools menu. If you click on it, your Output window should display a clickable message, which will open "D:\Project Files\CIS3G\Webapp_Test_BLL\Evaluation\Reports\TestEvaluationHistoryBLL.cs" if it exists, and will put the cursor on line 27 if it exists (if not, line 1) at column 44 if it exists (if not, column 1).
You can go further and add a shortcut key to your custom tool.
Select Tools->Options...->Environment->Keyboard. Find your tool in the list of commands. It will show as Tools.ExternalCommand[x] where [x] is its position in the list of External Tools. Enter your chosen shortcut key(s) in the Press shortcut keys: box and click OK.
Here is an article on doing this written in 2004 that still applies today:
http://www.codeproject.com/Articles/6176/Using-the-Output-Window-in-DevStudio
Yes, I wrote it, but as was pointed out earlier, there is nothing particularly magic about this. The article mentions VC6 and VC7, but I still use this technique in VS2005, VS2008, and VS2010 for the output of PC-Lint.

How can I add a custom command to Visual Studio?

There's an external command I'm using constantly - basically launching a batch file. I'd like to put a toolbar button (and possibly keyboard shortcut) for this into the IDE. Is there an easy way to do that?
First, add an item to your Tools menu by choosing Tools, External Tools, and filling out the dialog, like this:
Then bring up Tools Customize, click the Keyboard button at the bottom, and find the appropriate external tool number:
All the configured external commands in VS are available to add to a Toolbar or Menu through the Tools>Customize menu as "External Command ##". You just need to know which number corresponds to the particular command you've configured. I believe it is in order of entry in the external commands list.
Similarly, you can find those names in the commands list in Options>Environment>Keyboard and assign a shortcut to your command that way.

Easier way to copy the path to a file or directory to the clipboard in visual studio?

To accomplish this I've been right-clicking the file or directory and then selecting Show in Windows Explorer, and then copying the path out of the address bar.
Is there an add-on or macro that can make this task easier?
I'm aware that you can drag a file from Solution Explorer into the editor and it will write out the path, but when doing web development I don't always want the server path, sometimes I want the local path.
Look at PowerCommands extension
http://visualstudiogallery.msdn.microsoft.com/e5f41ad9-4edc-4912-bca3-91147db95b99?SRC=Home
Among others, it contains "Copy Path" command for files and folders
UPD: Also, another option: right-click the tab of open document and there will be "Copy Full Path" command. It's built-in, no extensions needed
You can just select the file in the 'Solution Explorer' file list and hit CTRL+C. It automatically copies the file path to your clipboard.
I've found one more option of how to solve it without plugins (for VS 2015 and above; not certain about previous versions)
Go to "Tools" -> "Customize..." -> "Commands" tab.
Choose "Context menu:" and "Project and Solution Context Menus | Item" and you can add any existing Command.
In order to copy path, choose Category "File" and "Copy Full Path" command. Will add button only for files in Solution Explorer.
This works for projects as well but you should choose "Project and Solution Context Menus | Project" (copy path of .vcxproj file).
It doesn't work for solutions and I don't understand actually why.
Tools > Options > Environment > Keyboard
You can set your own shortcut keys for anything. The two that are most relevant to this post are "File.CopyFullPath" and "File.CopyRelativePath".
I created my shortcut to just be used in "Text Editor" and it has worked great.
This was asked for VS2010 and I'm using VS2019, so it is an old question, but still relevant. I knew about the file header right click menu, but for some reason I didn't think about the shortcut keys until I read this question. Posting in case this results in a quicker fix for anyone else.
The CTRL+C doesn't work for me on MSVS 2017.
Use the copy as path extension here.
Then right click on file in solution explorer -> Copy as path (or assign a shortcut).
I find that it works very well.

Visual Studio - Run Batch File

What I would like
I would like to easily run a batch file without leaving Visual Studio. The batch files are not always the same, they change depending on the solution I'm working with.
What I know so far
I know you can create custom shortcuts in the Tools section of Visual Studio (Under External Tools).
My solutions have various scripts/batch files and I wish there was a way to create shortcuts under a solution folder or some other place that is solution specific.
I can create the scripts but there doesn't seem to be a way to run the script. I don't like having to open Windows Explorer each time I want to run a script.
Perhaps an add-in that would start a process with the selected file in the solution explorer? Or something similar?
Here is a full steps on how to add the external tool to run the batch files by right click on the file and select "Run the batch file", also whenever you need to edit the file, just open and edit it.
Here's how to do it...
Create an external tool called "Run batch file"
1) From Tools-> External Tools, create a new and put the below parameters:
2) Set the Command to: CMD.EXE
3) Set the Arguments to: /c "$(ItemPath)"
4) Set the Initial directory to: $(ItemDir)
5) ![DO NOT Check the "use output window" check box and then Apply to create the command
Note where the new command appeared in the list of commands.
The external commands are numbered from 1 starting below the divider bar.
#1 is usually "Create GUID"
To make it easy to remember you can move the new command to the top, to be the number one command in the list.][1]
6) Now go to Tools -> Customize and select the commands tab.
7) Select the Context menu radio button and select "Project and Solution Context menus | Item" from the drop down.
8) Now use "Add Command..." to add a new command
9) In the Categories list select "Tools"
10) From the commands select the "External Command #" that corresponds to the position of the "Run Batch file" custom command you noted the number of in step 5 above.
11) Move it to the right position in the list add keyboard short cuts etc.
12) Close the dialogue.
Now right click on the batch file and you should see a "Run batch file" menu item. This will execute th batch file and show it's output in the VS Output window.
Hope it helps.
I did this by right clicking the batch file and choosing "Open With", then I added a new editor and used explorer.exe (and then set that to be the default editor).
The best way to proceed is to write an external tool, and then you can pass in parameters based on your current solution that is loaded.
There are many project and solution specific variables you can pass to your external tool.
Open up the 'External Tools' dialog, and select your tool in the list box.
You will see the title of your external tool, as well as the command that points to the script or batch script you want to call.
The arguments edit box has a button next to it with an arrow next to it. Click the arrow and you will see a big list of variables, or parameters, you can pass to your external tool.
So, for instance you can use the following:
$(ProjectDir) - The full path directory of the project you are working on.
for instance "C:\builds\myproject"
$(ProjectPath) - The full path name of the vcproj you are working on.
for instance "C:\builds\myproject\foo.vcproj"
$(ProjectName) - The name of the project.
for instance "foo"
$(SolutionDir) - The full path directory of the solution that is currently loaded.
for instance "C:\builds\mysolution"
etc...
Will the Build Event hooks (pre-build, pre-link, post-build) be of any use to you? Also you can check out the Custom build setup too. Those are part of the solution.
You can add a new Makefile project to your solution. A Makefile project is a simple Visual Studio project whose build action is any command line you want. It is listed under the Visual C++ - General category in the new project dialog. In your case, just set the build command to invoke your batch script. Then, to execute your script while working in your solution, just right click on the Makefile project in the Solution Explorer and choose the Build context menu item.
Since you intend to run this script run on-demand (as opposed to running each time you build) you will want to remove it from the build in the Configuration Manager. Don't forget to disable building the project for all platforms and configurations.
Most of the macros are available to the Makefile project's build command line, although perhaps some that are associated with your main project will not have the correct value.
On my environment (Visual Studio 2022 v17.3.6 on Windows 11 v21H2) the $(ItemPath) parameter for cmd.exe leads to an error, but it works properly without:
This seems reasonable to me as the command is called "Open With" and works like the Windows "Open With" command by autmatically passing the file as an argument.

Resources