How to run a .bat from inside the IDE - visual-studio-2010

What I want to do is very simple, yet can't find the way to do it.
In my projects I have several .bat files (I know, I'm old) the perform some tasks like running SqlMetal, etc.
I just want to right-click the file and select "run" or something so the bat is executed. How do I do it?
Note: I know I can use Tools -> External Tools, but it's not what I'm looking for

If each project has a few batch files associated with it then why not include them in the project and add a new external tool and link it with a custom menu item to run the tool? This will give you a list of batch files in each project and a context menu command to run them. The only downside is that it isn't file type specific (it will let you run any file as a batch file).
Here's how to do it...
Create an external tool called "Run batch file"
Set the Command to: CMD.EXE
Set the Arguments to: /c "$(ItemPath)"
Set the Initial directory to: $(ItemDir)
Check the "use output window" checkbox 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"
Now go to Tools -> Customize and select the commands tab.
Select the Context menu radio button and select "Project and Solution Context menus | Item" from the dropdown.
Now use "Add Command..." to add a new command
In the Categories list select "Tools"
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.
Move it to the correct position in the list add keyboard shortcuts etc.
Close the dialog.
Now right click on the batch file and you should see a "Run batch file" menu item. This will execute the batch file and show its output in the VS Output window.

Right click the batch file in the Solution Explorer
Select “Open With…” from the context menu
Click “Add…”
In the “Program name” textbox, enter powershell.exe
In the “Friendly name” textbox enter “PowerShell”
Select “Set As Default”
Click OK
via

Check out the Open Command Line Visual Studio extension.
As per the description, it features:
Opening of a command line at the root of the project.
Support for all consoles such as CMD, PowerShell, Bash etc.
Syntax highlighting, Intellisense and execution of .cmd and .bat files.
For files in your solution, a context-menu button shows up.
Alternatively, the keyboard shortcut Shift+Alt+5 can be used when editing a batch file. This makes it really easy and fast to execute any batch file - even ones that are not part of your project.

Right-click a file in the Solution Explorer window, Open With, click the Add button to add your .bat files. Use %1 in your .bat to get the path to the selected file.

Couldn't get any of the above to quite work, but this did (VS 2019/2022):
Right click a .bat file in Solution Explorer and Open With...
Add...
Program: CMD.exe
Arguments: /c %1
Friendly name: Command
OK and Set as Default for double clicking of batch files to run from Solution Explorer as the default (or right click Open With... and select Command if you don't want it the default).

Merely a suggestion: This is a scenario which can be done with the VsCommandBuddy extension. The following configuration is an example:
{
"cmdname": "mk",
"title": "Run MK.BAT",
"description": "Run the MK bat file.",
"cwd": "$(SolutionDir)",
"filename": "cmd.exe",
"arguments": "/C mk.bat",
"async": false
}
You can assign shortkeys as well, so you can invoke your batch files with shortcut keys. Output comes in your output pane.

Related

How to add cmd command execution to right click menu on Windows?

I’m trying to add a menu button when I right click a file and the button will let me execute a command. To be more specific, I’m trying to right click a apk file, and the I can execute a “adb install” command of the selected file. I did some search and found out to add a menu item we need to modify the registry. But I’m not sure how to execute a command and moreover, how can I get the current selected file in right click?
Any help will be much appreciated!
Read about Verbs and File Associations on MSDN.
If you don't need it to be the default command, adding it under SystemFileAssociations is a good idea and works on Windows XP and later.
A minimal .reg file might look like this:
REGEDIT4
[HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.apk\shell\AdbInstall]
#="ADB Install"
[HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.apk\shell\AdbInstall\command]
#="\"C:\\path\\to\\androidsdk\\adb.exe\" install \"%1\""
%1 is replaced with the full path of the file you right-click on.

How to make the cmd line executable

Here's the situation, every time I need to do the step below after I boot my computer:
Open the cmd(admin)
copy this cmd into cmd:
C:\Windows\system32\netsh int ipv4 set glob defaultcurhoplimit=65
enter
For now, I hope to double click a file that can directly execute the command above without the step above.
Any method?
You can save it to .bat or .cmd file and run it on double-click.
Create a file with any name and extension .bat I.g. setLimit.bat. I assume you know how to create files.
Open file from above step in file editor, i.g. notepad.exe. Copy and paste line C:\Windows\system32\netsh int ipv4 set glob defaultcurhoplimit=65 into the file and press Ctrl+S to safe it. I hope it makes sense otherwise ask.
Double click on the file to execute the command.
If you need to run it under elevated privilege (i.e. admin) right click on file and choose Run as administrator option in menu.
You need a .bat file to execute the command and a shortcut to have it executed as administrator.
Creating the .bat file is trivial: use you favorite text editor (or notepad), type the command, optionnaly add a line PAUSE if you want to see eventual messages and save it to disk with a .bat extension (say c:\...\sethoplimit.bat)
Creating the shortcut is not much harder: right click on desktop, Create new..., Shortcut, and just select the newly created file
The magic comes now: right click on the newly created shortcut and click Properties, in Shortcut tab, click the Advanced button and select Execute as admin.
That's all. Each time you double click on the shortcut, Windows will start a console with elevated priviledges, eventually open the User Account Control confirmation dialog, and execute the batch file as administrator.
Here I showed you how to create the shortcut directly on the Desktop, but the same process can be used for a shortcut anywhere on a disk folder.

Is there a way to open command prompt in current folder?

I have a project folder somewhere, and many times I need to open command prompt (on windows 7/8.1/10) and cd to that folder. It would be very handy if there's an option right click > open cmd here.
is there a tool for that?
Just go to your folder location and type "cmd" on the address bar.
Press Shift + Menu, then choose Open Command Window Here. It works.
In win7/8, try right click while holding shift key, should have cmd prompt here
How about to use the PowerShell OpenHere module?
Run PowerShell with elevated permissions and type:
Install-Module OpenHere; Import-Module OpenHere
Set-OpenHereShortcut -ShortcutType:WindowsPowerShell
Disclaimer:
I'm a developer of this module.
use the description in https://stackoverflow.com/a/34534874/1498669 and put "run as administrator" checkbox on
Open 'File Explorer' and enter "shell:sendto" in location bar + hit return to get into this folder
Right mouse click and create a "New"->"Shortcut", put cmd.exe into the path, done
Edit the properties of the shortcut and edit the target to "%windir%\system32\cmd.exe /k pushd"
you can also create a copy of that shortcut, rename it to cmd.exe_admin, select properties, hit button "Advanced" an select the checkbox "run as Administrator"
Now use the "SendTo"-submenu in the explorer right click context menu to send any filepath or folder into the normal or elevated commandbox.
This "shell:sendto"-trick is also very useful for eg. Notepad++ or any program you want to start in "elevated" mode.
Type cmd command in address bar where path or location of the file or folder is shown.

.pl files open in notepad

I am new to xampp, and also, I am new to Perl programming.
I just tried to open the file C:\xampp\cgi-bin\printenv.pl and I made a mistake.
I chose the default program to open that file extension as Notepad, with the option always use the selected program to open this kind of file.
Now when I try to run .pl files from cmd it opens them in Notepad instead of executing. Please reply. Thanks in advance.
Type the following at a shell prompt:
assoc .pl=PerlScript
ftype PerlScript="c:\...\bin\perl.exe" "%1" %*
(Replace ... with the correct path.)
ikegami's suggestion did not work for me in Windows 10, but after finding Jack Wu's comment at ActivePerl. .pl files no longer execute but open in Notepad instead I was able to solve the problem as such:
Select and right-click a .pl file
Use the "Open With" submenu to "Choose another app"
Select "Always use this app to open .pl files" (do this now – you won't get the chance after you have selected a program)
Scroll to the bottom of the "Other options" to find "More apps", and select "Look for another app on this PC"
Navigate to C:/path/to/perl/bin/ and select Perl5.16.3.exe (or the equivalent, depending on which version of Perl you have installed: but not Perl.exe)
Then the Perl icon appears next to .pl files and a double-click leads to them opening in Perl every time, as desired.

BAT file to open CMD in current directory

I have many scripts which I interact with from the command line. Everytime I need to use them, I have to open a command line window and copy+paste and CD to the path to the directory they are in. This is tedious (they are in a rather deep file system, so typing out the full path is a pain, copy+paste is better but not much). I tried to create a .BAT file that I could double-click on that would open a new command-line window in the folder the .bat file exists in but it does not work. It opens a new window, but the working directory is not the directory that .bat file is in. Here's what I've got after much googling (My cmd skills ain't so great):
cd %CD%
cmd.exe
I know from when I used Linux that Konqueror had a "Command-line window here" feature, and that's the effect I'm trying to get on Windows.
you probably want to do this:
cd /d %~dp0
cmd.exe
this will set your current directory to the directory you have the batch file in
Create a file named open_dos_here.cmd with the following lines:
%~d1
cd "%~p1"
call cmd
Put this file at any folder.
Then, go to your Send To folder (Win+E; Alt+D;shell:sendto;Enter).
Create a shortcut to point to this open_dos_here.cmd
Then, in any folder, select any file or sub-folder. Right-click and select "Send To" and then select open_dos_here.cmd to open the DOS in that folder.
You can just enter cmd into the address bar in Explorer and it starts up in that path. Likewise for PowerShell.
There's a simpler way -
start /d "folder path"
As a more general solution you might want to check out the Microsoft Power Toy for XP that adds the "Open Command Window Here" option when you right-click: http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx
In Vista and Windows 7, you'll get that option if you hold down shift and right-click (this is built in).
I'm thinking that if you are creating a batch script that relies on the Current Directory being set to the folder that contains the batch file, that you are setting yourself up for trouble when you try to execute the batch file using a fully qualified path as you would from a scheduler.
Better to add this line to your batch file too:
REM Change Current Directory to the location of this batch file
CD /D %~dp0
unless you are fully qualifying all of your paths.
Another solution is to use a shortcut file to cmd.exe instead of a batch file.
Edit the shortcut's start in property to %~dp0.
You achieve the same thing, except it has the Cmd icon (and you can change this).
Some people don't like clicking on batch files without knowing what's in them, and some corporate network drives have a ban on .bat files...
The simplest command to do this:
start
You can always run this in command line to open new command line window in the same location. Or you can place it in your .bat file.
Most simple way in explorer is to Shift + right mouse click on the folder or on an empty space in the folder and click on Open command prompt here.
CMD will then start in that folder
I must say, I'm not sure if it works for Windows Vista and below, but it surely works for Windows 7, 8, 8.1 and 10.
Referring to answer of #Chris,
We can also go to parent directory of batch file and run commands using following
cd /d %~dp0..
<OTHER_BATCH_COMMANDS>
cmd.exe
To understand working of command cd /d %~dp0.. please refer below link
What does it mean by command cd /d %~dp0 in Windows
You could add a context menu entry through the registry:
Navigate in your Registry to
HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell
and create a key called "Command Prompt" without the quotes.
Set the default string to whatever text you want to appear in the right-click menu.
Create a new key within your newly created command prompt named "command," and set the default string to
cmd.exe /k pushd %1
You may need to add %SystemRoot%\system32\ before the cmd.exe if the executable can't be found.
The changes should take place immediately. Right click a folder and your new menu item should appear.
Also see http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm
When you are in the desired folder , just type CMD in your address bar
A bit late to the game but if I'm understanding your needs correctly this will help people with the same issue.
Two solutions with the same first step:
First navigate to the location you keep your scripts in and copy the filepath to that directory.
First Solution:
Click "Start"
Right-click "Computer" (or "My Computer)
Click "Properties"
On the left, click "Advanced System Settings"
Click "Environment Variables"
In the "System Variables" Box, scroll down and select "PATH"
Click "Edit"
In the "Variable Value" field, scroll all the way to the right
If there isn't a semi-colon (;) there yet, add it.
Paste in the filepath you copied earlier.
End with a semi-colon.
Click "OK"
Click "OK" again
Click "OK" one last time
You can now use any of your scripts as if you were already that folder.
Second Solution: (can easily be paired with the first for extra usefulness)
On your desktop create a batch file with the following content.
#echo off
cmd /k cd "C:\your\file\path"
This will open a command window like what you tried to do.
For tons of info on windows commands check here: http://ss64.com/nt/
Create a new file startCmdLine.bat in your directory and put this line in it
call cmd
That is it. Now double click on the .bat file. It works for me.
You can replace call with start, it will also work.
this code works for me
name it cmd.bat
#echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop
you can try:
shift + right click
then, click on Open command prompt here
Inside given folder click on the top Adddress Bar and type cmd and click enter
It will open command prompt with current folder address.
You can simply create a bat file in any convenient place and drop any file from the desired directory onto it.
Haha. Code for this:
cmd

Resources