1

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

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
pc1oad1etter
  • 8,549
  • 10
  • 49
  • 64

2 Answers2

2

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:>
RBerteig
  • 41,948
  • 7
  • 88
  • 128
  • 1
    Sorry, but you are assuming that cmd.exe is too much like a *nix shell. Each batch file invoked at a command prompt is interpreted by that very copy of cmd.exe and can indeed change the environment for other commands. That is why setlocal was added. You are correct if a batch file is invoked from explorer, however. This has been true since DOS 1.0 and COMMAND.COM, and remains true at least in XP. – RBerteig Apr 25 '09 at 06:03
0

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
BenV136
  • 319
  • 2
  • 5