Question

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).

Was it helpful?

Solution

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:>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top