批处理脚本中的非模态通知框
我想要一个名为“形成批处理文件”的非模式警报框。 目前我正在使用 vbscript 创建模式警报框:
>usermessage.vbs ECHO WScript.Echo^( "Generating report - this may take a moment." ^)
WSCRIPT.EXE usermessage.vbs
但我想继续执行脚本(生成报告)而不等待用户交互。 我怎样才能做到这一点? 我不在乎它是否是 vbscript - 我只是希望它能够从我的批处理脚本(在 Windows XP 中)运行。
I would like to have a non-modal alert box called form a batch file. Currently I am using vbscript to create a modal alert box:
>usermessage.vbs ECHO WScript.Echo^( "Generating report - this may take a moment." ^)
WSCRIPT.EXE usermessage.vbs
But I would like to proceed with the script (generating the report) without waiting for user interaction. How can I accomplish this? I don't care if it's vbscript or not - I just want it to work from my batch script (in Windows XP).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个对当前技巧没有新要求的简单答案是使用
start
命令将脚本与批处理文件执行分离。这可能看起来像:
唯一的区别是使用
start
运行wscript
。 这确实存在一个缺陷,即它会在当前目录中留下临时文件,并且该框最终需要手动关闭。这两个问题都很容易处理:
这里,我将临时脚本移至
%TMP%
文件夹,并记住在完成后将其删除。 我使用了 echo 和 ping 命令来浪费一些时间来演示长时间运行的进程。 而且,我使用了wscript
的/I
和/T
选项来确保脚本“交互式”运行并设置最大值允许脚本运行的时间。@echo off
和setlocal
使其在命令提示符下运行时看起来更干净,并防止它在提示符环境中留下名称“%msg%”。编辑: Johannes Rössel 在评论中对
setlocal
的批评是不正确的。 如果在命令提示符处调用此命令,则在没有setlocal
的情况下,变量 msg 对提示符以及从该提示符启动的其他批处理文件和程序将是可见的。 如果实际上编写的不仅仅是一次性脚本,那么使用 setlocal 来隔离批处理文件中的局部变量是一种很好的做法。这可以很容易地证明:
A simple answer that has no new requirements over your current trick is to use the
start
command to detach the script from the batch file execution.That might look something like:
where the only difference is using
start
to runwscript
. This does suffer from the defect that it leaves a temporary file laying around in the current directory, and that the box does need to be eventually manually dismissed.Both issues are easy to handle:
Here, I move the temporary script over to the
%TMP%
folder, and remember to delete it when we're done with it. I used anecho
and aping
command to waste some time to demonstrate a long process running. And, I used the/I
and/T
options towscript
to make certain that the script is run "interactively" and to set a maximum time to allow the script to run.The
@echo off
andsetlocal
make it look cleaner when running at a command prompt and prevent it from leaving the name `%msg% in the prompt's environment.Edit: Johannes Rössel's criticism of
setlocal
in the comments is incorrect. If this is invoked at a command prompt, without thesetlocal
the variable msg will be visible to the prompt and to other batch files and programs launched from that prompt. It is good practice to usesetlocal
to isolate local variables in a batch file if one is actually writing anything more than a throw-away script.This can be easily demonstrated:
我也遇到了同样的问题,我有一个 VBScript 需要弹出一个消息框,但不能停止并继续。 我想在一段时间后关闭。 这对我有用。 本质上,一个 vbscript 创建另一个 vbscript 文件,然后使用 shellexecute 执行该消息框文件,超时时间为 15 秒。
I struggled with this same issue, I had a VBScript that needed to pop up a message box but not stop and continue on. And I wanted to close after a certain amount of time. This worked for me. Essentially one vbscript creates another vbscript file and then uses shellexecute to execute that message box file with a timeout of say 15 seconds.