I added a batch (.bat) file to my project. I want to add a new editor for batch files that would actually execute this batch file in command prompt window.
我在我的项目中添加了一个批处理(.bat)文件。我想为批处理文件添加一个新的编辑器,它将在命令提示符窗口中实际执行此批处理文件。
What I did, but didn't remember my editor:
我做了什么,但不记得我的编辑:
- Right clicked on my .bat file
- Selected Open with...
- Clicked Add
- Set program as "cmd /c" and some Friendly name
- Accepted this dialog.
- A new window appears that tells me it can't validate editor path (because of the additinoal parameter, because if I remove it, it works)
- My new editor is in the window.
右键单击我的.bat文件
选择开放...
将程序设置为“cmd / c”和一些友好名称
接受此对话框。
出现一个新窗口,告诉我它无法验证编辑器路径(因为additinoal参数,因为如果我删除它,它可以工作)
我的新编辑器在窗口中。
When I open the same Open with... dialog again, my editor isn't present.
当我再次打开相同的Open with ...对话框时,我的编辑器不存在。
How should I tackle this?
我应该怎么解决这个问题?
5 个解决方案
#1
Ok. I managed to solve this myself.
As I found on the internet this is a Visual Studio bug, so it doesn't remember these kind of editors (parametrised executable file names). But nonetheless. The solution goes like this:
好。我自己设法解决了这个问题。正如我在互联网上发现的那样,这是一个Visual Studio错误,所以它不记得这些编辑器(参数化的可执行文件名)。但是,尽管如此。解决方案是这样的:
- Create a separate batch file i.e. ExecuteBatch.cmd
-
Put this code in it:
把这段代码放进去:
@cmd /c %1
@cmd / c%1
-
Then add the new editor in Visual Studio by pointing to this ExecuteBatch.cmd file.
然后通过指向此ExecuteBatch.cmd文件在Visual Studio中添加新编辑器。
创建一个单独的批处理文件,即ExecuteBatch.cmd
Voila. Problem solved. Detailed instructions can be found in this blog post.
瞧。问题解决了。详细说明可以在这篇博客文章中找到。
#2
According to the advice here you can also use "Open With" PowerShell, and you don't have to worry about any parameters.
根据这里的建议,你也可以使用“打开方式”PowerShell,你不必担心任何参数。
The instructions from that link are:
该链接的说明如下:
You can run .bat files from the solution explorer directly by associating the default behavior of “.BAT” extensions to Powershell.
您可以通过将“.BAT”扩展名的默认行为与Powershell相关联,直接从解决方案资源管理器运行.bat文件。
- Right click the batch file in the Solution Explorer
在解决方案资源管理器中右键单击批处理文件
- Select “Open With…” from the context menu
从上下文菜单中选择“打开方式...”
- Click “Add…”
- In the “Program name” textbox , specify the full path of PowerShell (“\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”)
在“程序名称”文本框中,指定PowerShell的完整路径(“\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe”)
- In the “Friendly name” textbox enter “PowerShell”
在“友好名称”文本框中输入“PowerShell”
- Select “Set As Default”
选择“设为默认值”
- Click OK
Now when you double click your batch file in the solution explorer, it will shell out to powershell and execute.
现在,当您在解决方案资源管理器中双击批处理文件时,它将转出PowerShell并执行。
Works for me.
适合我。
#3
Expanding upon the answers by Robert and Rustam, here is the RunBatch.bat script that I use as the "Open With" program for running batch files from Visual Studio:
扩展了Robert和Rustam的答案,这里是RunBatch.bat脚本,我用它作为“Open With”程序从Visual Studio运行批处理文件:
@cd %~dp1
@call "%~n1%~x1"
The first line navigates to the file's folder and the second line runs the file.
第一行导航到文件的文件夹,第二行导航文件。
This preserves the working directory in Visual Studio 2012 and later, and also supports batch files that have spaces in the file name.
这将保留Visual Studio 2012及更高版本中的工作目录,并且还支持文件名中包含空格的批处理文件。
#4
Additional suggestion: in VS2012 you can just use cmd.exe.
附加建议:在VS2012中,您只需使用cmd.exe即可。
But! The drawback is that your bat file will start in VS home folder, not in your soultion's folder. That's why I used this recipe, but extended it a little. My .bat file is:
但!缺点是你的bat文件将在VS home文件夹中启动,而不是在你的soultion的文件夹中。这就是为什么我使用这个配方,但延长了一点。我的.bat文件是:
:: Navigate to file's folder
@cd %~dp1
:: Run file in its folder
@cmd /c %~n1%~x1
Syntax is explained here
这里解释语法
#5
I just told visual studio to open the .bat file with windows explorer ;)
我刚刚告诉visual studio用windows explorer打开.bat文件;)
#1
Ok. I managed to solve this myself.
As I found on the internet this is a Visual Studio bug, so it doesn't remember these kind of editors (parametrised executable file names). But nonetheless. The solution goes like this:
好。我自己设法解决了这个问题。正如我在互联网上发现的那样,这是一个Visual Studio错误,所以它不记得这些编辑器(参数化的可执行文件名)。但是,尽管如此。解决方案是这样的:
- Create a separate batch file i.e. ExecuteBatch.cmd
-
Put this code in it:
把这段代码放进去:
@cmd /c %1
@cmd / c%1
-
Then add the new editor in Visual Studio by pointing to this ExecuteBatch.cmd file.
然后通过指向此ExecuteBatch.cmd文件在Visual Studio中添加新编辑器。
创建一个单独的批处理文件,即ExecuteBatch.cmd
Voila. Problem solved. Detailed instructions can be found in this blog post.
瞧。问题解决了。详细说明可以在这篇博客文章中找到。
#2
According to the advice here you can also use "Open With" PowerShell, and you don't have to worry about any parameters.
根据这里的建议,你也可以使用“打开方式”PowerShell,你不必担心任何参数。
The instructions from that link are:
该链接的说明如下:
You can run .bat files from the solution explorer directly by associating the default behavior of “.BAT” extensions to Powershell.
您可以通过将“.BAT”扩展名的默认行为与Powershell相关联,直接从解决方案资源管理器运行.bat文件。
- Right click the batch file in the Solution Explorer
在解决方案资源管理器中右键单击批处理文件
- Select “Open With…” from the context menu
从上下文菜单中选择“打开方式...”
- Click “Add…”
- In the “Program name” textbox , specify the full path of PowerShell (“\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”)
在“程序名称”文本框中,指定PowerShell的完整路径(“\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe”)
- In the “Friendly name” textbox enter “PowerShell”
在“友好名称”文本框中输入“PowerShell”
- Select “Set As Default”
选择“设为默认值”
- Click OK
Now when you double click your batch file in the solution explorer, it will shell out to powershell and execute.
现在,当您在解决方案资源管理器中双击批处理文件时,它将转出PowerShell并执行。
Works for me.
适合我。
#3
Expanding upon the answers by Robert and Rustam, here is the RunBatch.bat script that I use as the "Open With" program for running batch files from Visual Studio:
扩展了Robert和Rustam的答案,这里是RunBatch.bat脚本,我用它作为“Open With”程序从Visual Studio运行批处理文件:
@cd %~dp1
@call "%~n1%~x1"
The first line navigates to the file's folder and the second line runs the file.
第一行导航到文件的文件夹,第二行导航文件。
This preserves the working directory in Visual Studio 2012 and later, and also supports batch files that have spaces in the file name.
这将保留Visual Studio 2012及更高版本中的工作目录,并且还支持文件名中包含空格的批处理文件。
#4
Additional suggestion: in VS2012 you can just use cmd.exe.
附加建议:在VS2012中,您只需使用cmd.exe即可。
But! The drawback is that your bat file will start in VS home folder, not in your soultion's folder. That's why I used this recipe, but extended it a little. My .bat file is:
但!缺点是你的bat文件将在VS home文件夹中启动,而不是在你的soultion的文件夹中。这就是为什么我使用这个配方,但延长了一点。我的.bat文件是:
:: Navigate to file's folder
@cd %~dp1
:: Run file in its folder
@cmd /c %~n1%~x1
Syntax is explained here
这里解释语法
#5
I just told visual studio to open the .bat file with windows explorer ;)
我刚刚告诉visual studio用windows explorer打开.bat文件;)