windows 如何从批处理文件运行程序,而无需在程序启动后打开控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/324539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:47:31  来源:igfitidea点击:

How can I run a program from a batch file without leaving the console open after the program starts?

windowsbatch-file

提问by Mister Dev

For the moment my batch file look like this:

目前我的批处理文件如下所示:

myprogram.exe param1

The program starts but the DOS Window remains open. How can I close it?

程序启动,但 DOS 窗口保持打开状态。我怎样才能关闭它?

采纳答案by Patrick Desjardins

You can use the exit keyword. Here is an example from one of my batch files:

您可以使用 exit 关键字。这是我的批处理文件之一的示例:

start myProgram.exe param1
exit

回答by Marshall

Use the start command to prevent the batch file from waiting for the program. Just remember to put a empty double quote in front of the program you want to run after "Start". For example, if you want to run Visual Studio 2012 from a batch command:

使用 start 命令可以防止批处理文件等待程序。请记住在“开始”之后要运行的程序前加上一个空双引号。例如,如果要从批处理命令运行 Visual Studio 2012:

Start ""  "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"

notice the double quote after start.

注意开始后的双引号。

回答by Lasse V. Karlsen

Look at the START command, you can do this:

看START命令,可以这样做:

START rest-of-your-program-name

For instance, this batch-file will wait until notepad exits:

例如,此批处理文件将等到记事本退出:

@echo off
notepad c:\test.txt

However, this won't:

但是,这不会:

@echo off
start notepad c:\test.txt

回答by VonC

From my own question:

从我自己的问题

start /b myProgram.exe params...

works if you start the program from an existing DOS session.

如果您从现有的 DOS 会话启动该程序,则该程序有效。

If not, call a vb script

如果没有,请调用 vb 脚本

wscript.exe invis.vbs myProgram.exe %*

The Windows Script Host Run() methodtakes:

Windows脚本宿主Run()方法需要:

  • intWindowStyle : 0 means "invisible windows"
  • bWaitOnReturn : false means your first script does not need to wait for your second script to finish
  • intWindowStyle : 0 表示“不可见的窗口”
  • bWaitOnReturn : false 意味着您的第一个脚本不需要等待您的第二个脚本完成

Here is invis.vbs:

这是 invis.vbs:

set args = WScript.Arguments
num = args.Count

if num = 0 then
    WScript.Echo "Usage: [CScript | WScript] invis.vbs aScript.bat <some script arguments>"
    WScript.Quit 1
end if

sargs = ""
if num > 1 then
    sargs = " "
    for k = 1 to num - 1
        anArg = args.Item(k)
        sargs = sargs & anArg & " "
    next
end if

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False

回答by Zosimas

This is the only thing that worked for me when I tried to run a java class from a batch file:

当我尝试从批处理文件运行 java 类时,这是唯一对我有用的方法:

start "cmdWindowTitle" /B "javaw" -cp . testprojectpak.MainForm

start "cmdWindowTitle" /B "javaw" -cp . testprojectpak.MainForm

You can customize the startcommand as you want for your project, by following the proper syntax:

您可以start按照正确的语法为您的项目自定义命令:

Syntax
      START "title" [/Dpath] [options] "command" [parameters]

Key:
   title      : Text for the CMD window title bar (required)
   path       : Starting directory
   command    : The command, batch file or executable program to run
   parameters : The parameters passed to the command

Options:
   /MIN       : Minimized
   /MAX       : Maximized
   /WAIT      : Start application and wait for it to terminate
   /LOW       : Use IDLE priority class
   /NORMAL    : Use NORMAL priority class
   /HIGH      : Use HIGH priority class
   /REALTIME  : Use REALTIME priority class

   /B         : Start application without creating a new window. In this case
                ^C will be ignored - leaving ^Break as the only way to 
                interrupt the application
   /I         : Ignore any changes to the current environment.

   Options for 16-bit WINDOWS programs only

   /SEPARATE   Start in separate memory space (more robust)
   /SHARED     Start in shared memory space (default)

回答by Chris Dail

You should try this. It starts the program with no window. It actually flashes up for a second but goes away fairly quickly.

你应该试试这个。它在没有窗口的情况下启动程序。它实际上会闪烁一秒钟,但很快就会消失。

start "name" /B myprogram.exe param1

回答by Gilco

How to solve "space problem" and local dependencies:

如何解决“空间问题”和本地依赖:

@echo off
cd "C:\Program Files\HeidiSQL"
start heidisql.exe

cd "C:\Program Files (x86)\Google\Chrome\Application"
start chrome.exe

exit

回答by Gerhard Barnard

Loads of answers for this question already, but I am posting this to clarify something important, though this might not always be the case:

这个问题的答案已经很多了,但我发布这个是为了澄清一些重要的事情,尽管情况可能并不总是这样:

Start "C:\Program Files\someprog.exe"

Might cause issues in some windows versions as Startactually expects the first set of quotation marks to be a windows title. So it is best practice to first double quote a comment, or a blank comment:

可能会在某些 Windows 版本中导致问题,因为Start实际上期望第一组引号是 Windows 标题。因此,最佳做法是首先双引号注释或空白注释:

Start "" "C:\Program Files\someprog.exe"

or

或者

Start "Window Title" "C:\Program Files\someprog.exe"

回答by Ilyich

Here is my preferred solution. It is taken from an answerto a similar question.

这是我的首选解决方案。它是从拍摄的回答到类似的问题。

Use a VBS Script to call the batch file:

使用 VBS 脚本调用批处理文件:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\path\to\your\batchfile.bat" & Chr(34), 0
Set WshShell = Nothing

Copy the lines above to an editor and save the file with .VBS extension.

将上面的行复制到编辑器并使用 .VBS 扩展名保存文件。

回答by Midas

My solution to do this from the GUI:

我从 GUI 执行此操作的解决方案:

  1. Create a shortcut to the program you want to run;

  2. Edit the shortcut's properties;

  3. Change the TARGETfield to %COMSPEC% /C "START "" "PROGRAMNAME"";

  4. Change the RUNfield to minimized.

  1. 创建要运行的程序的快捷方式;

  2. 编辑快捷方式的属性;

  3. TARGET字段更改为%COMSPEC% /C "START "" "PROGRAMNAME"";

  4. 将该RUN字段更改为最小化。

Ready! See how you like it...

准备好!看你喜欢...

PS: Program parameters can be inserted in between the two final quotation marks; the PROGRAMNAMEstring can be either a filename, a relative or an absolute path -- if you put in an absolute path and erase the drive letter and semicolon, then this will work in a thumbdrive no matter what letter the host computer assigns to it... (also, if you place the shortcut in the same folder and precede the program filename in PROGRAMNAMEwith the %CD%variable, paths will always match; same trick can be used in START INfield).

PS:最后两个引号之间可以插入程序参数;该PROGRAMNAME字符串可以是文件名、相对路径或绝对路径——如果您输入绝对路径并擦除驱动器号和分号,那么无论主机分配给它什么字母,这都将在拇指驱动器中工作。 . (此外,如果您将快捷方式放在同一个文件夹中并在程序文件名之前PROGRAMNAME使用%CD%变量,路径将始终匹配;可以在START IN字段中使用相同的技巧)。