Windows 批处理脚本启动程序并退出控制台 [英] Windows batch script launch program and exit console

查看:35
本文介绍了Windows 批处理脚本启动程序并退出控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于启动程序的批处理脚本,例如 notepad.exe.当我双击这个批处理文件时,记事本正常启动,但启动notepad.execmd的黑色窗口仍然在后台.我该怎么做才能启动 notepad.exe 并使 cmd 窗口消失?

I have a batch script that I use to launch a program, such as notepad.exe. When I double click on this batch file, notepad starts normally, but the black window of the cmd who launched notepad.exe remains in the background. What do I have to do in order to launch notepad.exe and make the cmd window disappear?

edit:比使用 I 更复杂.

cmd 调用cygwincygwin 启动notepad.我用

start I pathcygwininash.exe

第一个窗口 (cmd) 消失了,但第二个窗口 (cygwininash.exe) 仍在后台.在 cygwin 脚本中,我使用了 notepad.exe & 然后退出.

and the first window (cmd) disappears, but a second window (cygwininash.exe) is still on the background. In the cygwin script I used notepad.exe & and then exit.

推荐答案

start "" "%SystemRoot%Notepad.exe"

"" 保持在开始和应用程序路径之间.

Keep the "" in between start and your application path.

添加说明:

通常,当我们从如下所示的批处理文件启动程序时,我们会像 OP 所说的那样在后台显示黑色窗口.

Normally when we launch a program from a batch file like below, we'll have the black windows at the background like OP said.

%SystemRoot%Notepad.exe

这是由记事本在同一命令提示符(进程)中运行引起的.命令提示符将在记事本关闭后关闭.为了避免这种情况,我们可以使用 start 命令来启动这样的单独进程.

This was cause by Notepad running in same command prompt (process). The command prompt will close AFTER notepad is closed. To avoid that, we can use the start command to start a separate process like this.

start %SystemRoot%Notepad.exe

这个命令没问题,只要它在路径中没有空格.为了以防万一,为了处理路径中的空间,我们添加了这样的 " 引号.

This command is fine as long it doesn't has space in the path. To handle space in the path for just in case, we added the " quotes like this.

start "%SystemRoot%Notepad.exe"

然而,运行此命令只会启动另一个空白命令提示符.为什么?如果您查找 start/?start 命令会将 " 之间的参数识别为新命令提示符的标题将要启动.所以,为了解决这个问题,我们有这样的命令:

However running this command would just start another blank command prompt. Why? If you lookup to the start /?, the start command will recognize the argument between the " as the title of the new command prompt it is going to launch. So, to solve that, we have the command like this:

start "" "%SystemRoot%Notepad.exe"

""的第一个参数是设置标题(我们设置为空白),第二个参数是"%SystemRoot%Notepad.exe" 是要运行的目标命令(支持路径中的空格).

The first argument of "" is to set the title (which we set as blank), and the second argument of "%SystemRoot%Notepad.exe" is the target command to run (that support spaces in the path).

如果需要在命令中添加参数,只需将它们加上引号即可,即:

If you need to add parameters to the command, just append them quoted, i.e.:

start "" "%SystemRoot%Notepad.exe" "<filename>" 

这篇关于Windows 批处理脚本启动程序并退出控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆