如何在 Windows 8 中启动使用任务计划程序最小化的批处理文件?- %comspec% 方法在 Windows 7 之后不再起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18881221/
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 10:53:44  来源:igfitidea点击:

How to start a batch file minimized with task scheduler in Windows 8? - %comspec% method not working anymore after Windows 7

windowsbatch-filecmd

提问by user225479

After Windows XP, I always use the trick below to start batch files minimized with Windows Task Manager.

在 Windows XP 之后,我总是使用下面的技巧来启动使用 Windows 任务管理器最小化的批处理文件。

From http://www.pcreview.co.uk/forums/running-bat-files-minimized-scheduler-t2125918.html:

http://www.pcreview.co.uk/forums/running-bat-files-minimized-scheduler-t2125918.html

"prequisite: all your batch files have an exit-command to finish the actions off. If you do not exit, you will end with a command prompt blinking.

“前提条件:您的所有批处理文件都有一个退出命令来完成操作。如果您不退出,您将以命令提示符闪烁结束。

This is what I keep using:

这是我一直在使用的:

%comspec% /c start /min "C:\Scripts\Destination_inbound_ftp5.bat"

When you save this in the properties, you will get a follow-up dialogue asking you if you meant all this to be parameters or not. Answer NO and the task will be saved as you would expect.

当您将其保存在属性中时,您将看到一个后续对话框,询问您是否将所有这些都作为参数。回答“否”,任务将按预期保存。

I also read the Stack Overflow question “start %comspec% /c script.cmd” vs “start cmd /C second.cmd script.cmd”, which made me replace the "%comspec%" statement with "C:\Windows\system32\cmd.exe", but that did not change anything either.

我还阅读了堆栈溢出问题“start %comspec% /c script.cmd” vs “start cmd /C second.cmd script.cmd”,这让我将“%comspec%”语句替换为“C:\Windows\ system32\cmd.exe”,但这也没有改变任何东西。

The problem is that now, instead of a minimized running bat file, I end up with just a command prompt, minimized but without any of the batch commands executed. The task scheduler status remains "running" :(

问题是,现在,而不是最小化的运行 bat 文件,我最终只有一个命令提示符,最小化但没有执行任何批处理命令。任务调度程序状态保持“正在运行”:(

How do I get this done on Windows 8 (64-bit)? Preferrable with old-school batch commands instead of PowerShell (or worse ;p)

如何在 Windows 8(64 位)上完成此操作?最好使用老式批处理命令而不是 PowerShell(或更糟;p)

采纳答案by foxidrive

The start command needs the leading ""quotes to disable the title feature. Try scheduling this:

start 命令需要前导""引号来禁用标题功能。尝试安排这个:

%comspec% /c start "" /min "C:\Scripts\Destination_inbound_ftp5.bat"

回答by RationalRabbit

Assuming Windows 8 is the same as Windows 7, an "exit" is only going to exit the batch file (which it is going to do anyway).

假设 Windows 8 与 Windows 7 相同,“退出”只会退出批处理文件(无论如何它都会这样做)。

You need to add the exit code like this:

您需要像这样添加退出代码:

Under "Program/Script":

在“程序/脚本”下:

CMD (or command.exe, or %comspec%)

CMD(或 command.exe,或 %comspec%)

Under "Arguments:

在“参数:

/c start "Title" /min "C:\Scripts\Destination_inbound_ftp5.bat" ^& exit

回答by Neil C. Obremski

I didn't like seeing the command window pop up and then disappear so here is another solution from https://ss64.com/vb/run.html...

我不喜欢看到命令窗口弹出然后消失,所以这里是https://ss64.com/vb/run.html 的另一个解决方案......

First create invisible.vbswith this single line of text:

首先invisible.vbs使用这一行文本创建:

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Next and finally, launch your cmd or batch file via:

接下来,最后,通过以下方式启动您的 cmd 或批处理文件:

%SystemRoot%\system32\wscript.exe "invisible.vbs" "myscript.cmd" //nologo

Ta da! Scripting of this sort has been built into Windows for a long time. If you're curious, do a web search for "WSH" (windows scripting host). You can even write such scripts in dialect of JavaScript called JScript.

哒哒!长期以来,这种脚本已内置于 Windows 中。如果您好奇,请在网络上搜索“WSH”(Windows 脚本主机)。您甚至可以使用称为 JScript 的 JavaScript 方言编写此类脚本。

回答by Sopalajo de Arrierez

Another possibility: a small freeware program named CMDH, that simply runs the requested orders in background. For example:

另一种可能性:一个名为CMDH的小型免费软件程序,它只是在后台运行请求的订单。例如:

cmdh MyScript.cmd

No need to add "exit" to the script. Tested working in Windows XP SP3, and there is no reason it should fail on Windows 8.

无需在脚本中添加“退出”。在 Windows XP SP3 中测试工作,并且没有理由在 Windows 8 上失败。

回答by Flion

Maybe it's not the same as you mean but if I simply run a .bat file with the scheduler and tick this "Hidden" box on the General tab of the task properties it starts minified.

也许它与您的意思不同,但是如果我只是使用调度程序运行一个 .bat 文件并在任务属性的常规选项卡上勾选这个“隐藏”框,它就会开始缩小。

enter image description here

在此处输入图片说明

回答by balazer

Here's a solution from https://ss64.com/vb/run.htmlthat will run a batch file in a minimized window. Unlike the other solutions that use the startcommand with /min, this one will not flash a new window onto your screen or interrupt full-screen activities. It does, however, steal focus. I don't know how to avoid that.

这是来自https://ss64.com/vb/run.html的解决方案,它将在最小化窗口中运行批处理文件。与使用带start命令的其他解决方案不同,这个解决方案/min不会在您的屏幕上闪烁新窗口或中断全屏活动。然而,它确实会窃取焦点。我不知道如何避免这种情况。

First create a file named run_minimized.vbswith this single line of text:

首先创建一个以run_minimized.vbs这一行文本命名的文件:

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 2, False

Next, create your Task Scheduler task with an action to start the program wscript.exewith these arguments:

接下来,创建您的 Task Scheduler 任务,并wscript.exe使用以下参数启动程序:

"c:\path\run_minimized.vbs" "c:\path\my script.bat"

Change the paths as necessary to specify the locations of the two files.

根据需要更改路径以指定两个文件的位置。

There is no simple way to pass arguments from Task Scheduler to the batch file while also preserving spaces and quotation marks, because wscript strips quotation marks from its arguments. The simplest way to handle arguments with spaces would be to put the entire batch file command into the vbs:

没有简单的方法可以将参数从 Task Scheduler 传递到批处理文件,同时还要保留空格和引号,因为 wscript 会从其参数中去除引号。处理带空格参数的最简单方法是将整个批处理文件命令放入 vbs:

CreateObject("Wscript.Shell").Run  """c:\path\my script.bat"" ""arg 1"" arg2", 2, False

Note the use of quotation marks. There's one pair of quotation marks "enclosing the entire command string, and a pair of adjacent quote characters ""every place you'd use a normal quotation mark in a command line.

注意引号的使用。有一对引号"将整个命令字符串括起来,""在命令行中使用普通引号的每个地方都有一对相邻的引号字符。