【发布时间】:2010-09-24 09:12:37
【问题描述】:
目前我的批处理文件如下所示:
myprogram.exe param1
程序启动,但 DOS 窗口仍然打开。如何关闭它?
【问题讨论】:
-
您可能应该将接受的答案更改为 Marshall 发布的答案。
标签: windows batch-file
目前我的批处理文件如下所示:
myprogram.exe param1
程序启动,但 DOS 窗口仍然打开。如何关闭它?
【问题讨论】:
标签: windows batch-file
您可以使用 exit 关键字。这是我的一个批处理文件中的一个示例:
start myProgram.exe param1
exit
【讨论】:
exit 放入批处理文件中。
start "VPN" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
看START命令,可以这样操作:
START rest-of-your-program-name
例如,这个批处理文件将等到记事本退出:
@echo off
notepad c:\test.txt
但是,这不会:
@echo off
start notepad c:\test.txt
【讨论】:
你应该试试这个。它启动没有窗口的程序。它实际上闪烁了一秒钟,但很快就消失了。
start "name" /B myprogram.exe param1
【讨论】:
"title" 选项很重要。如果程序路径包含空格,则必须用引号括起来,因此我们必须添加"title" 以避免失败。
来自我的own question:
start /b myProgram.exe params...
如果您从现有的 DOS 会话启动程序,则可以工作。
如果没有,调用vb脚本
wscript.exe invis.vbs myProgram.exe %*
Windows Script Host Run() method 需要:
这里是 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
【讨论】:
我从 GUI 执行此操作的解决方案:
为要运行的程序创建快捷方式;
编辑快捷方式的属性;
将TARGET字段更改为%COMSPEC% /C "START "" "PROGRAMNAME"";
将RUN 字段更改为最小化。
准备好了!看看你喜欢它...
PS:程序参数可以在最后两个引号之间插入; PROGRAMNAME 字符串可以是文件名、相对路径或绝对路径——如果您输入绝对路径并擦除驱动器号和分号,那么无论主机分配什么字母,这都可以在拇指驱动器中使用它...(另外,如果您将快捷方式放在同一个文件夹中并在PROGRAMNAME 中的程序文件名之前加上%CD% 变量,路径将始终匹配;同样的技巧可以在START IN 字段中使用)。
【讨论】:
当我尝试从批处理文件运行 java 类时,这是唯一对我有用的东西:
start "cmdWindowTitle" /B "javaw" -cp . testprojectpak.MainForm
您可以按照正确的语法为您的项目自定义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)
【讨论】:
使用启动命令防止批处理文件等待程序。只要记住在“开始”之后要运行的程序前面加上一个空的双引号。 例如,如果您想通过批处理命令运行 Visual Studio 2012:
Start "" "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"
注意开始后的双引号。
【讨论】:
start <window name> <program name>
如果您希望按计划或始终运行此批处理文件;您可以使用 Windows 计划工具,它在启动批处理文件时不会在窗口中打开。
打开Task Scheduler:
'cmd'
taskschd.msc -> 回车从右侧点击Create Basic Task 并按照菜单进行操作。
希望这会有所帮助。
【讨论】:
如何解决“空间问题”和本地依赖:
@echo off
cd "C:\Program Files\HeidiSQL"
start heidisql.exe
cd "C:\Program Files (x86)\Google\Chrome\Application"
start chrome.exe
exit
【讨论】:
这是我的首选解决方案。它取自answer 到一个类似的问题。
使用 VBS 脚本调用批处理文件:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\path\to\your\batchfile.bat" & Chr(34), 0
Set WshShell = Nothing
将以上行复制到编辑器并使用 .VBS 扩展名保存文件。
【讨论】:
这个问题已经有很多答案了,但我发布这个是为了澄清一些重要的事情,尽管情况可能并非总是如此:
Start "C:\Program Files\someprog.exe"
在某些 Windows 版本中可能会导致问题,因为 Start 实际上期望第一组引号是 Windows 标题。因此,最佳做法是先对评论或空白评论进行双引号:
Start "" "C:\Program Files\someprog.exe"
或
Start "Window Title" "C:\Program Files\someprog.exe"
【讨论】: