批处理脚本中的非模态通知框

发布于 2024-07-17 08:39:24 字数 307 浏览 9 评论 0原文

我想要一个名为“形成批处理文件”的非模式警报框。 目前我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-07-24 08:39:24

一个对当前技巧没有新要求的简单答案是使用 start 命令将脚本与批处理文件执行分离。

这可能看起来像:

>usermessage.vbs ECHO WScript.Echo^( "Generating report - this may take a moment." ^)
start WSCRIPT.EXE usermessage.vbs
echo This text is a proxy for the hard work of writing the report

唯一的区别是使用 start 运行 wscript。 这确实存在一个缺陷,即它会在当前目录中留下临时文件,并且该框最终需要手动关闭。

这两个问题都很容易处理:

@echo off
setlocal 
set msg="%TMP%\tempmsg.vbs"
ECHO WScript.Echo^( "Generating report - this may take a moment." ^) >%msg% 
start WSCRIPT.EXE /I /T:15 %msg%
echo This text is a proxy for the hard work of writing the report
ping -n 5 127.0.0.1 >NULL
del %msg% >NUL 2>&1

这里,我将临时脚本移至 %TMP% 文件夹,并记住在完成后将其删除。 我使用了 echo 和 ping 命令来浪费一些时间来演示长时间运行的进程。 而且,我使用了 wscript/I/T 选项来确保脚本“交互式”运行并设置最大值允许脚本运行的时间。

@echo offsetlocal 使其在命令提示符下运行时看起来更干净,并防止它在提示符环境中留下名称“%msg%”。

编辑: Johannes Rössel 在评论中对 setlocal 的批评是不正确的。 如果在命令提示符处调用此命令,则在没有 setlocal 的情况下,变量 msg 对提示符以及从该提示符启动的其他批处理文件和程序将是可见的。 如果实际上编写的不仅仅是一次性脚本,那么使用 setlocal 来隔离批处理文件中的局部变量是一种很好的做法。

这可以很容易地证明:

C:> type seta.bat 
@set A=SomeValue

C:> set A
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\Ross\Application Data

C:> seta.bat

C:> set A
A=SomeValue
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\Ross\Application Data

C:>

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:

>usermessage.vbs ECHO WScript.Echo^( "Generating report - this may take a moment." ^)
start WSCRIPT.EXE usermessage.vbs
echo This text is a proxy for the hard work of writing the report

where the only difference is using start to run wscript. 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:

@echo off
setlocal 
set msg="%TMP%\tempmsg.vbs"
ECHO WScript.Echo^( "Generating report - this may take a moment." ^) >%msg% 
start WSCRIPT.EXE /I /T:15 %msg%
echo This text is a proxy for the hard work of writing the report
ping -n 5 127.0.0.1 >NULL
del %msg% >NUL 2>&1

Here, I move the temporary script over to the %TMP% folder, and remember to delete it when we're done with it. I used an echo and a ping command to waste some time to demonstrate a long process running. And, I used the /I and /T options to wscript to make certain that the script is run "interactively" and to set a maximum time to allow the script to run.

The @echo off and setlocal 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 the setlocal 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 use setlocal 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:

C:> type seta.bat 
@set A=SomeValue

C:> set A
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\Ross\Application Data

C:> seta.bat

C:> set A
A=SomeValue
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\Ross\Application Data

C:>
乱世争霸 2024-07-24 08:39:24

我也遇到了同样的问题,我有一个 VBScript 需要弹出一个消息框,但不能停止并继续。 我想在一段时间后关闭。 这对我有用。 本质上,一个 vbscript 创建另一个 vbscript 文件,然后使用 shellexecute 执行该消息框文件,超时时间为 15 秒。

Set wshShell = CreateObject( "WScript.Shell" )
tmpPath = wshShell.ExpandEnvironmentStrings( "%TMP%" )
set wshShell = Nothing

msgFile = tmpPath & "\tempmsg.vbs"
Set fs = CreateObject("Scripting.FileSystemObject")
Set objFile = fs.CreateTextFile(msgFile,True)
objFile.Write "MsgBox " & chr(34) & "It worked!" & chr(34) & vbCrLf
objFile.Close
Set fs = Nothing

dim objShell
set objShell = CreateObject("shell.application")
objShell.ShellExecute "cscript.exe", "/I /T:15 " & (char34) & msgFile & chr(34), "", "open", 0
set objShell = Nothing

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.

Set wshShell = CreateObject( "WScript.Shell" )
tmpPath = wshShell.ExpandEnvironmentStrings( "%TMP%" )
set wshShell = Nothing

msgFile = tmpPath & "\tempmsg.vbs"
Set fs = CreateObject("Scripting.FileSystemObject")
Set objFile = fs.CreateTextFile(msgFile,True)
objFile.Write "MsgBox " & chr(34) & "It worked!" & chr(34) & vbCrLf
objFile.Close
Set fs = Nothing

dim objShell
set objShell = CreateObject("shell.application")
objShell.ShellExecute "cscript.exe", "/I /T:15 " & (char34) & msgFile & chr(34), "", "open", 0
set objShell = Nothing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文