Show a popup/message box from a Windows batch file - windows

Is there a way to display a message box from a batch file (similar to how xmessage can be used from bash-scripts in Linux)?

First of all, DOS has nothing to do with it, you probably want a Windows command line solution (again: no DOS, pure Windows, just not a Window, but a Console).
You can either use the VBScript method provided by boflynn or you can mis-use net send or msg. net send works only on older versions of windows:
net send localhost Some message to display
This also depends on the Messenger service to run, though.
For newer versions (XP and onward, apparently):
msg "%username%" Some message to display
It should be noted that a message box sent using msg.exe will only last for 60 seconds. This can however be overridden with the /time:xx switch.

I would make a very simple VBScript file and call it using CScript to parse the command line parameters.
Something like the following saved in MessageBox.vbs:
Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox messageText
Which you would call like:
cscript MessageBox.vbs "This will be shown in a popup."
MsgBox reference if you are interested in going this route.

Might display a little flash, but no temp files required. Should work all the way back to somewhere in the (IIRC) IE5 era.
mshta javascript:alert("Message\n\nMultiple\nLines\ntoo!");close();
Don't forget to escape your parentheses if you're using if:
if 1 == 1 (
mshta javascript:alert^("1 is equal to 1, amazing."^);close^(^);
)

This will pop-up another Command Prompt window:
START CMD /C "ECHO My Popup Message && PAUSE"

Try :
Msg * "insert your message here"
If you are using Windows XP's command.com, this will open a message box.
Opening a new cmd window isn't quite what you were asking for, I gather.
You could also use VBScript, and use this with your .bat file. You would open it from the bat file with this command:
cd C:\"location of vbscript"
What this does is change the directory command.com will search for files from, then on the next line:
"insert name of your vbscript here".vbs
Then you create a new Notepad document, type in
<script type="text/vbscript">
MsgBox "your text here"
</script>
You would then save this as a .vbs file (by putting ".vbs" at the end of the filename), save as "All Files" in the drop down box below the file name (so it doesn't save as .txt), then click Save!

Few more ways.
1) The geekiest and hackiest - it uses the IEXPRESS to create small exe that will create a pop-up with a single button (it can create two more types of pop-up messages). Works on EVERY windows from XP and above:
;#echo off
;setlocal
;set ppopup_executable=popupe.exe
;set "message2=click OK to continue"
;
;del /q /f %tmp%\yes >nul 2>&1
;
;copy /y "%~f0" "%temp%\popup.sed" >nul 2>&1
;(echo(FinishMessage=%message2%)>>"%temp%\popup.sed";
;(echo(TargetName=%cd%\%ppopup_executable%)>>"%temp%\popup.sed";
;(echo(FriendlyName=%message1_title%)>>"%temp%\popup.sed"
;
;iexpress /n /q /m %temp%\popup.sed
;%ppopup_executable%
;rem del /q /f %ppopup_executable% >nul 2>&1
;pause
;endlocal
;exit /b 0
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=1
HideExtractAnimation=1
UseLongFileName=0
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[SourceFiles]
SourceFiles0=C:\Windows\System32\
[SourceFiles0]
%FILE0%=
[Strings]
AppLaunched=subst.exe
PostInstallCmd=<None>
AdminQuietInstCmd=
UserQuietInstCmd=
FILE0="subst.exe"
DisplayLicense=
InstallPrompt=
2) Using MSHTA. Also works on every windows machine from XP and above (despite the OP do not want "external" languages the JavaScript here is minimized). Should be saved as .bat:
#if (true == false) #end /*!
#echo off
mshta "about:<script src='file://%~f0'></script><script>close()</script>" %*
goto :EOF */
alert("Hello, world!");
or in one line:
mshta "about:<script>alert('Hello, world!');close()</script>"
or
mshta "javascript:alert('message');close()"
or
mshta.exe vbscript:Execute("msgbox ""message"",0,""title"":close")
3) Here's parameterized .bat/jscript hybrid (should be saved as bat). It again uses JavaScript despite the OP request but as it is a bat it can be called as a bat file without worries. It uses POPUP which allows a little bit more control than the more popular MSGBOX. It uses WSH, but not MSHTA like in the example above.
#if (#x)==(#y) #end /***** jscript comment ******
#echo off
cscript //E:JScript //nologo "%~f0" "%~nx0" %*
exit /b 0
#if (#x)==(#y) #end ****** end comment *********/
var wshShell = WScript.CreateObject("WScript.Shell");
var args=WScript.Arguments;
var title=args.Item(0);
var timeout=-1;
var pressed_message="button pressed";
var timeout_message="timed out";
var message="";
function printHelp() {
WScript.Echo(title + "[-title Title] [-timeout m] [-tom \"Time-out message\"] [-pbm \"Pressed button message\"] [-message \"pop-up message\"]");
}
if (WScript.Arguments.Length==1){
runPopup();
WScript.Quit(0);
}
if (args.Item(1).toLowerCase() == "-help" || args.Item(1).toLowerCase() == "-h" ) {
printHelp();
WScript.Quit(0);
}
if (WScript.Arguments.Length % 2 == 0 ) {
WScript.Echo("Illegal arguments ");
printHelp();
WScript.Quit(1);
}
for (var arg = 1 ; arg<args.Length;arg=arg+2) {
if (args.Item(arg).toLowerCase() == "-title") {
title = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-timeout") {
timeout = parseInt(args.Item(arg+1));
if (isNaN(timeout)) {
timeout=-1;
}
}
if (args.Item(arg).toLowerCase() == "-tom") {
timeout_message = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-pbm") {
pressed_message = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-message") {
message = args.Item(arg+1);
}
}
function runPopup(){
var btn = wshShell.Popup(message, timeout, title, 0x0 + 0x10);
switch(btn) {
// button pressed.
case 1:
WScript.Echo(pressed_message);
break;
// Timed out.
case -1:
WScript.Echo(timeout_message);
break;
}
}
runPopup();
4) and one jscript.net/.bat hybrid (should be saved as .bat) .This time it uses .NET and compiles a small .exe file that could be deleted:
#if (#X)==(#Y) #end /****** silent jscript comment ******
#echo off
::::::::::::::::::::::::::::::::::::
::: compile the script ::::
::::::::::::::::::::::::::::::::::::
setlocal
::if exist "%~n0.exe" goto :skip_compilation
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
if exist "%%v\jsc.exe" (
rem :: the javascript.net compiler
set "jsc=%%~dpsnfxv\jsc.exe"
goto :break_loop
)
)
echo jsc.exe not found && exit /b 0
:break_loop
call %jsc% /nologo /out:"%~n0.exe" "%~f0"
::::::::::::::::::::::::::::::::::::
::: end of compilation ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation
::
::::::::::
"%~n0.exe" %*
::::::::
::
endlocal
exit /b 0
****** end of jscript comment ******/
import System;
import System.Windows;
import System.Windows.Forms
var arguments:String[] = Environment.GetCommandLineArgs();
MessageBox.Show(arguments[1],arguments[0]);
5) and at the end one single call to powershell that creates a pop-up (can be called from command line or from batch if powershell is installed):
powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show("""Hello World""", """My PopUp Message Box""")
6) And the dbenham's approach seen here
start "" cmd /c "echo(&echo(&echo Hello world! &echo(&pause>nul"
7) For a system tray notifications you can try this:
call SystemTrayNotification.bat -tooltip warning -time 3000 -title "Woow" -text "Boom" -icon question

This way your batch file will create a VBS script and show a popup. After it runs, the batch file will delete that intermediate file.
The advantage of using MSGBOX is that it is really customaziable (change the title, the icon etc) while MSG.exe isn't as much.
echo MSGBOX "YOUR MESSAGE" > %temp%\TEMPmessage.vbs
call %temp%\TEMPmessage.vbs
del %temp%\TEMPmessage.vbs /f /q

Here's a PowerShell variant that doesn't require loading assemblies prior to creating the window, however it runs noticeably slower (~+50%) than the PowerShell MessageBox command posted here by #npocmaka:
powershell (New-Object -ComObject Wscript.Shell).Popup("""Operation Completed""",0,"""Done""",0x0)
You can change the last parameter from "0x0" to a value below to display icons in the dialog (see Popup Method for further reference):
0x10 Stop
0x20 Question Mark
0x30 Exclamation Mark
0x40 Information Mark
Adapted from the Microsoft TechNet article PowerTip: Use PowerShell to Display Pop-Up Window.

echo X=MsgBox("Message Description",0+16,"Title") >msg.vbs
–you can write any numbers from 0,1,2,3,4 instead of 0 (before the ‘+’ symbol) & here is the meaning of each number:
0 = Ok Button
1 = Ok/Cancel Button
2 = Abort/Retry/Ignore button
3 = Yes/No/Cancel
4 = Yes/No
–you can write any numbers from 16,32,48,64 instead of 16 (after the ‘+’ symbol) & here is the meaning of each number:
16 – Critical Icon
32 – Warning Icon
48 – Warning Message Icon
64 – Information Icon

Msg * "insert your message here"
works fine, just save as a .bat file in notepad or make sure the format is set to "all files"

msg * /time:0 /w Hello everybody!
This message waits forever until OK is clicked (it lasts only one minute by default) and works fine in Windows 8.1

In order to do this, you need to have a small program that displays a messagebox and run that from your batch file.
You could open a console window that displays a prompt though, but getting a GUI message box using cmd.exe and friends only is not possible, AFAIK.

Following on #Fowl's answer, you can improve it with a timeout to only appear for 10 seconds using the following:
mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'Message!', 10, 'Title!', 64 );close()"
See here for more details.

You can invoke dll function from user32.dll i think
Something like
Rundll32.exe user32.dll, MessageBox (0, "text", "titleText", {extra flags for like topmost messagebox e.t.c})
Typing it from my Phone, don't judge me... otherwise i would link the extra flags.

I use a utility named msgbox.exe from here:
http://www.paulsadowski.com/WSH/cmdprogs.htm

You can use Zenity, which posts regular releases on download.gnome.org. Zenity allows for the execution of dialog boxes in command-line and shell scripts. More info can also be found on Wikipedia.
It is cross-platform, but I couldn't find a recent win32 build. Unfortunately placella.com went offline. A Windows installer of v3.20 (from March 2016) can be found here.

This application can do that, if you convert (wrap) your batch files into executable files.
Simple Messagebox
%extd% /messagebox Title Text
Error Messagebox
%extd% /messagebox Error "Error message" 16
Cancel Try Again Messagebox
%extd% /messagebox Title "Try again or Cancel" 5
4) "Never ask me again" Messagebox
%extd% /messageboxcheck Title Message 0 {73E8105A-7AD2-4335-B694-94F837A38E79}

Here is my batch script that I put together based on the good answers here & in other posts
You can set title timeout & even sleep to schedule it for latter & \n for new line
Name it popup.bat & put it in your windows path folder to work globally on your pc
For example popup Line 1\nLine 2 will produce a 2 line popup box
(type popup /? for usage)
Here is the code
<!-- : Begin CMD
#echo off
cscript //nologo "%~f0?.wsf" %*
set pop.key=[%errorlevel%]
if %pop.key% == [-1] set pop.key=TimedOut
if %pop.key% == [1] set pop.key=Ok
if %pop.key% == [2] set pop.key=Cancel
if %pop.key% == [3] set pop.key=Abort
if %pop.key% == [4] set pop.key=Retry
if %pop.key% == [5] set pop.key=Ignore
if %pop.key% == [6] set pop.key=Yes
if %pop.key% == [7] set pop.key=No
if %pop.key% == [10] set pop.key=TryAgain
if %pop.key% == [11] set pop.key=Continue
if %pop.key% == [99] set pop.key=NoWait
exit /b
-- End CMD -->
<job><script language="VBScript">
'on error resume next
q =""""
qsq =""" """
Set objArgs = WScript.Arguments
Set objShell= WScript.CreateObject("WScript.Shell")
Popup = 0
Title = "Popup"
Timeout = 0
Mode = 0
Message = ""
Sleep = 0
button = 0
If objArgs.Count = 0 Then
Usage()
ElseIf objArgs(0) = "/?" or Lcase(objArgs(0)) = "-h" or Lcase(objArgs(0)) = "--help" Then
Usage()
End If
noWait = Not wait()
For Each arg in objArgs
If (Mid(arg,1,1) = "/") and (InStr(arg,":") <> 0) Then haveSwitch = True
Next
If not haveSwitch Then
Message=joinParam("woq")
Else
For i = 0 To objArgs.Count-1
If IsSwitch(objArgs(i)) Then
S=split(objArgs(i) , ":" , 2)
select case Lcase(S(0))
case "/m","/message"
Message=S(1)
case "/tt","/title"
Title=S(1)
case "/s","/sleep"
If IsNumeric(S(1)) Then Sleep=S(1)*1000
case "/t","/time"
If IsNumeric(S(1)) Then Timeout=S(1)
case "/b","/button"
select case S(1)
case "oc", "1"
button=1
case "ari","2"
button=2
case "ync","3"
button=3
case "yn", "4"
button=4
case "rc", "5"
button=5
case "ctc","6"
button=6
case Else
button=0
end select
case "/i","/icon"
select case S(1)
case "s","x","stop","16"
Mode=16
case "?","q","question","32"
Mode=32
case "!","w","warning","exclamation","48"
Mode=48
case "i","information","info","64"
Mode=64
case Else
Mode=0
end select
end select
End If
Next
End If
Message = Replace(Message,"/\n", "°" )
Message = Replace(Message,"\n",vbCrLf)
Message = Replace(Message, "°" , "\n")
If noWait Then button=0
Wscript.Sleep(sleep)
Popup = objShell.Popup(Message, Timeout, Title, button + Mode + vbSystemModal)
Wscript.Quit Popup
Function IsSwitch(Val)
IsSwitch = False
If Mid(Val,1,1) = "/" Then
For ii = 3 To 9
If Mid(Val,ii,1) = ":" Then IsSwitch = True
Next
End If
End Function
Function joinParam(quotes)
ReDim ArgArr(objArgs.Count-1)
For i = 0 To objArgs.Count-1
If quotes = "wq" Then
ArgArr(i) = q & objArgs(i) & q
Else
ArgArr(i) = objArgs(i)
End If
Next
joinParam = Join(ArgArr)
End Function
Function wait()
wait=True
If objArgs.Named.Exists("NewProcess") Then
wait=False
Exit Function
ElseIf objArgs.Named.Exists("NW") or objArgs.Named.Exists("NoWait") Then
objShell.Exec q & WScript.FullName & qsq & WScript.ScriptFullName & q & " /NewProcess: " & joinParam("wq")
WScript.Quit 99
End If
End Function
Function Usage()
Wscript.Echo _
vbCrLf&"Usage:" _
&vbCrLf&" popup followed by your message. Example: ""popup First line\nescaped /\n\nSecond line"" " _
&vbCrLf&" To triger a new line use ""\n"" within the msg string [to escape enter ""/"" before ""\n""]" _
&vbCrLf&"" _
&vbCrLf&"Advanced user" _
&vbCrLf&" If any Switch is used then you must use the /m: switch for the message " _
&vbCrLf&" No space allowed between the switch & the value " _
&vbCrLf&" The switches are NOT case sensitive " _
&vbCrLf&"" _
&vbCrLf&" popup [/m:""*""] [/t:*] [/tt:*] [/s:*] [/nw] [/i:*]" _
&vbCrLf&"" _
&vbCrLf&" Switch | value |Description" _
&vbCrLf&" -----------------------------------------------------------------------" _
&vbCrLf&" /m: /message:| ""1 2"" |if the message have spaces you need to quote it " _
&vbCrLf&" | |" _
&vbCrLf&" /t: /time: | nn |Duration of the popup for n seconds " _
&vbCrLf&" | |<Default> untill key pressed" _
&vbCrLf&" | |" _
&vbCrLf&" /tt: /title: | ""A B"" |if the title have spaces you need to quote it " _
&vbCrLf&" | | <Default> Popup" _
&vbCrLf&" | |" _
&vbCrLf&" /s: /sleep: | nn |schedule the popup after n seconds " _
&vbCrLf&" | |" _
&vbCrLf&" /nw /NoWait | |Continue script without the user pressing ok - " _
&vbCrLf&" | | botton option will be defaulted to OK button " _
&vbCrLf&" | |" _
&vbCrLf&" /i: /icon: | ?/q |[question mark]" _
&vbCrLf&" | !/w |[exclamation (warning) mark]" _
&vbCrLf&" | i/info|[information mark]" _
&vbCrLf&" | x/stop|[stop\error mark]" _
&vbCrLf&" | n/none|<Default>" _
&vbCrLf&" | |" _
&vbCrLf&" /b: /button: | o |[OK button] <Default>" _
&vbCrLf&" | oc |[OK and Cancel buttons]" _
&vbCrLf&" | ari |[Abort, Retry, and Ignore buttons]" _
&vbCrLf&" | ync |[Yes, No, and Cancel buttons]" _
&vbCrLf&" | yn |[Yes and No buttons]" _
&vbCrLf&" | rc |[Retry and Cancel buttons]" _
&vbCrLf&" | ctc |[Cancel and Try Again and Continue buttons]" _
&vbCrLf&" ---> | ---> |The output will be saved in variable ""pop.key""" _
&vbCrLf&"" _
&vbCrLf&"Example:" _
&vbCrLf&" popup /tt:""My MessageBox"" /t:5 /m:""Line 1\nLine 2\n/\n\nLine 4""" _
&vbCrLf&"" _
&vbCrLf&" v1.9 By RDR # 2020"
Wscript.Quit
End Function
</script></job>

Bat file:
#echo off
echo wscript.Quit((msgbox("question?",4+32+256, "title")-6) Mod 255) > %temp%\msgbox.vbs
start /wait %temp%\msgbox.vbs
rem echo wscript returned %errorlevel%
if errorlevel 1 goto error
echo We have Yes
goto end
:error
echo We have No
:end
del %temp%\msgbox.vbs /f /q

I would create a batch subroutine MSGBOX like shown below which you can call then via
call :MSGBOX "Test-Message 1" "Test-Title 1"
as often you want.
For example:
#ECHO OFF
:: call message box sub-routine
call :MSGBOX "Test-Message 1" "Test-Title 1"
call :MSGBOX "Test-Message 2" "Test-Title 2"
:END
EXIT /B
::::::::::::::::
:: sub-routines
:MSGBOX
:: 1. parameter: message
:: 2. parameter: title
:: find temporary name for VBS file
:uniqueLoop
set "uniqueFileName=%tmp%\msgbox~%RANDOM%.vbs"
if exist "%uniqueFileName%" goto :uniqueLoop
:: write to temporary VBS file, execute, delete file
echo msgbox"%~1",vbInformation , "%~2"> %uniqueFileName%
%uniqueFileName%
erase %uniqueFileName%
EXIT /B

msg * /server:127.0.0.1 Type your message here

A better option
set my_message=Hello world&& start cmd /c "#echo off & mode con cols=15 lines=2 & echo %my_message% & pause>nul"
Description:
lines= amount of lines,plus 1
cols= amount of characters in the message, plus 3 (However, minimum must be 15)
Auto-calculated cols version:
set my_message=Hello world&& (echo %my_message%>EMPTY_FILE123 && FOR %? IN (EMPTY_FILE123 ) DO SET strlength=%~z? && del EMPTY_FILE123 ) && start cmd /c "#echo off && mode con lines=2 cols=%strlength% && echo %my_message% && pause>nul"

it needs ONLY to popup when inside a vm, so technically, there should be some code like:
if %machine_type% == virtual_machine then
echo message box code
else
continue normal installation code

Related

findstr() Runtime error 23 when called as a subprocess?

I am attempting to run findstr command from within my vb6 project however it seems that I am in over my head in determining the correct syntax.
I was calling a batch file to run the findstr but trying to integrate it in the project is failing.
'''''''' Lines below allow ONLY Numeric text in teh Test1 box
Dim textval As String
Dim numval As String
Private Sub Text1_Change()
textval = Text1.Text
If IsNumeric(textval) Then
numval = textval
Else
Text1.Text = CStr(numval)
End If
End Sub
'''''''' Lines above allow ONLY Numeric text in teh Test1 box
Private Sub Command1_Click()
''''Lines below enables program/project to execute in the current `enter code here`directory
Dim MyCurrentDir As String
'Show current directory
MyCurrentDir = CurDir
' MsgBox "Current Directory is " & MyCurrentDir
''''Lines above enables program/project to execute in the current directory
' Remarked for testing below WORKS: Shell (MyCurrentDir & "\findstring.bat " & Text1)
' TEST BELOW
Dim command As String
'findstr "%1" *achineCont*.log | findstr "Q_OUTFEED_EJECTBAG_SIG">FOUNDBAG.txt
command = "findstr "" & Text1 & "" *achineCont*.log | findstr ""Q_OUTFEED_EJECTBAG_SIG"">FOUNDBAG.txt"
Shell "cmd.exe /c command"
'findstr "Done" *ai*.log | findstr "writing" | findstr "%1">>FOUNDBAG.txt
Command2 = "Done"" *ai*.log | findstr ""writing"" | findstr "" & Text1 & "">>FOUNDBAG.txt"
Shell "cmd.exe /c command2"
'start "" cmd /c cscript ReadTimeFromFileWriteToNewFile.vbs
Command3 = "start """" cmd /c cscript ReadNewFile.vbs"
Shell "cmd.exe /c command3"
' TEST ABOVE
End Sub
Private Sub Command2_Click()
Unload Form1 'tell the form to unload
Set Form1 = Nothing 'free the memory used by the variables
End Sub
Private Sub Form_Load()
''''Lines below enables program/project to execute in the current directory
Dim MyCurrentDir As String
'Show current directory
MyCurrentDir = CurDir
' MsgBox "Current Directory is " & MyCurrentDir
''''Lines above enables program/project to execute in the current directory
End Sub
You are several quotes away from being correct in each command. You need to triple up some of the quotes, and re-position some of the others. For the first command, try this:
Command = "findstr """ & Text1 & """ *achineCont*.log | findstr ""Q_OUTFEED_EJECTBAG_SIG"">FOUNDBAG.txt"
For the second command, try this:
Command2 = """Done"" *ai*.log | findstr ""writing"" | findstr """ & Text1 & """>>FOUNDBAG.txt"
And last but not least, for the fourth command, try this:
Command4 = "findstr ""Done"" *ai*.log | findstr ""writing"" | findstr """ & Text1 & """>>FOUNDBAG.txt"
A good way to figure these out is to display the string with either Debug.Print or a MsgBox and adjust quotes until the string is correct. Also, remember a string has opening and closing quotes. Any other quotes within the string need to be escaped by doubling up the quote.

control not returning to original vbs file after run command

I am trying to call a vbs file from another. The called file executes. However the control is not returning to the original vbs file. This file appears to be running as I see wscript process in task manager. But i don't see the output - the step after the run command. Any help/suggestion would be appreciated.
1.) vbs file 1 (original vbs file test3.vbs)
Set objShell = WScript.CreateObject ("WScript.shell")
strErrorCode = objShell.run("cmd /K C:\temp\a\test2.vbs", 0, True)
msgbox "complete test3"
Set objShell = Nothing
2.) vbs file 2 (called vbs file - test2.vbs)
msgbox "in test2"
WScript.Sleep 10000
msgbox "complete - test2"
3.) Expected output :
in test2
complete - test2
complete test3
4.) actual:
in test2
complete - test2
objShell.run("cmd /K C:\temp\a\test2.vbs", 0, True) never ends due to the /K switch:
cmd /?
Starts a new instance of the Windows command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
So cmd window remains open but it is hidden owing to the intWindowStyle parameter: 0 = Hides the window and activates another window, supposing the Run method syntax pattern as .Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Use either
strErrorCode = objShell.run("cmd /C C:\temp\a\test2.vbs", 0, True)
or
strErrorCode = objShell.run("wscript.exe C:\temp\a\test2.vbs", 0, True)
or even
strErrorCode = objShell.run("C:\temp\a\test2.vbs", 0, True)

VBS How to determine if a program is running

I need a way to determine if a process with a visible window is open, using VBScript.
For example, when I close the SolidWorks window, the SolidWorks.exe process remains running.
How can I find out which is which? Any suggestions?
Maybe you could use the command line program tasklist.exe to find out if the right window is open.
If you run tasklist /V /FI "IMAGENAME eq sldworks.exe" and find a difference between the process you are interested in and the other one, this might work.
Assuming there is a specific window title you can look for:
Dim pid = GetProcessId("sldworks.exe", "That window title")
If pid > 0 Then
MsgBox "Yay we found it"
End If
where GetProcessId() is this
Function GetProcessId(imageName, windowTitle)
Dim currentUser, command, output, tasklist, tasks, i, cols
currentUser = CreateObject("Wscript.Network").UserName
command = "tasklist /V /FO csv"
command = command & " /FI ""USERNAME eq " + currentUser + """"
command = command & " /FI ""IMAGENAME eq " + imageName + """"
command = command & " /FI ""WINDOWTITLE eq " + windowTitle + """"
command = command & " /FI ""SESSIONNAME eq Console"""
' add more or different filters, see tasklist /?
output = Trim(Shell(command))
tasklist = Split(output, vbNewLine)
' starting at 1 skips first line (it contains the column headings only)
For i = 1 To UBound(tasklist) - 1
cols = Split(tasklist(i), """,""")
' a line is expected to have 9 columns (0-8)
If UBound(cols) = 8 Then
GetProcessId = Trim(cols(1))
Exit For
End If
Next
End Function
Function Shell(cmd)
Shell = WScript.CreateObject("WScript.Shell").Exec(cmd).StdOut.ReadAll()
End Function
You don't have to return the PID, you could also return True/False or any other information tasklist provides. For reference, the tasklist column indexes are:
0: "Image Name"
1: "PID",
2: "Session Name"
3: "Session#"
4: "Mem Usage",
5: "Status"
6: "User Name"
7: "CPU Time"
8: "Window Title"
More advanced interaction with processes is available through the WMI. Plenty of examples how to use that in VBScript are all over the Internet. Search for Win32_Process.

VBS or Bat - Determine OS and Office Version

Does anyone have a script that can determine the Windows OS and Office Version in the same script.
I have bits and pieces of the script but I can't seem to figure out how to incorporate both OS and Office Version in the script. I started out in bat now I moved on to VBS as it seems to be able to provide more details however, if someone could just help out with logic in below I might be able to move forward.
I would like to know how I can setup a script like this.
If Windows 7 64bit & Office 2010
do this
If Windows XP 32bit & Office 2007
do this
If Windows 7 & Office 2007
do this
CODE FOR Detecting Windows Version -- BAT SCRIPT
Echo Please wait.... detecting Windows OS version...
ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto done
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp
ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto done
ver | find "NT" > nul
if %ERRORLEVEL% == 0 goto done
if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i
echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7
echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto done
echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_7
goto warnthenexit
Although the the Office part is kind of slow, it does work.
Just include this inside a file with a name like getversions.vbs
On my computer, it printed:
Microsoft Windows 8 Enterprise
Microsoft Office 32-bit Components 2013, Version15
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo objOperatingSystem.Caption
Next
Set colSoft = objWMIService.ExecQuery("SELECT * FROM Win32_Product WHERE Name Like 'Microsoft Office%'")
If colSoft.Count = 0 Then
wscript.echo "NO OFFFICE INSTALLED"
else
For Each objItem In colSoft
Wscript.echo objitem.caption & ", Version" & Left(objItem.Version, InStr(1,objItem.Version,".")-1)
exit for
Next
End If
Save this (VB Script) file as GetVersions.vbs.
It works
Regards,
Shaun
Option Explicit ' Enforce variable declaration
' Declare objects
Dim oShell
Dim sOSVersion
Dim lOfficeVersion
Set oShell = CreateObject("WScript.Shell")
On Error Resume Next
sOSVersion = oShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName")' Read the registry for the operating system version
lOfficeVersion = GetOfficeVersionNumber() ' Read the office version from the function
MsgBox "sOSVersion = " & sOSVersion & vbCrLf & "lOfficeVersion = " & lOfficeVersion
Function GetOfficeVersionNumber()
GetOfficeVersionNumber = "" ' or you could use "Office not installed"
Dim sTempValue
' Read the Classes Root registry hive (it is a memory-only instance amalgamation of HKCU\Software\Classes and HKLM\Software\Classes registry keys) as it contains a source of information for the currently active Microsoft Office Excel application major version - it's quicker and easier to read the registry than the file version information after a location lookup). The trailing backslash on the line denotes that the # or default registry key value is being queried.
sTempValue = oShell.RegRead("HKCR\Excel.Application\CurVer\")
If Len(sTempValue) > 2 Then GetOfficeVersionNumber = Replace(Right(sTempValue, 2), ".", "") ' Check the length of the value found and if greater than 2 digits then read the last two digits for the major Office version value
End Function ' GetOfficeVersionNumber

GUI for batch file? [duplicate]

This question already has answers here:
Windows Batch file launch a gui with buttons
(2 answers)
Closed 9 months ago.
I have a batch file, and it is a very simple program that launches website, a mini- web browser type experience, with commands that open programs, etc. How can i make an interface for this or a GUI? Without having to compleltey manually change my code. Here is an example of what my code is like:
:start
#echo off
COLOR 1E
cls
echo Welcome to Wannow Dashboard. This is the main page.
echo Type in the number to be redirected to your desired location.
echo 1. Useful Websites
echo 2. Programs
echo Wannow Dashboard created by Brad Wannow
set/p var1=
if %var1% == 1 goto Websites
if %var1% == 2 goto program
pause
exit
:websites
COLOR 1E
cls
echo Welcome to Wannow Dashboard: Websites. Select a command, type in number to be redirected.
echo 1. www.Pandora.com
echo 2. www.Google.com
echo 3. Aventa Blackboard
echo 4. Other
#echo OFF
#echo %time%
ping -n 1 -w 1 127.0.0.1 1>nul
echo Wannow Dashboard
Of course there is much more code, but this is the way my program is written, with also some START commands and user inputs, etc.
graphical commands are not available in straight batch files. I would suggest you look at vbscript or powershell
there are many guides - this is the help file for vbscript. yes it will be different. Echo Hello World would become msgbox("Hello World") and an input would look like inputbox("What is your name?") (at a very basic level)
there is no automatic conversion, and unless you have Visual Studio, no free integrated developer, but notepad++ seems to be the preferred editor because of its syntax highlighting
from here, an example script with a menu
'-----------------------------------------------------------------
' Name: Menu Template Script
' By: Harvey Colwell
' CopyRight: (c) Jul 2000, All Rights Reserved!
'
'*****************************************************************
Option Explicit
Dim oFS, oWS, oWN
Set oWS = WScript.CreateObject("WScript.Shell")
Set oWN = WScript.CreateObject("WScript.Network")
Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
'----------
' Script SetUp
'----------
'----------
' Main
'----------
Select Case InputBox ( _
"Enter menu item number then Click Ok. . ." & vbCrlf & vbCrlf & _
" [1] Item 1" & vbCrlf & _
" [2] Item 2" & vbCrlf & _
" [3] Item 3" & vbCrlf & _
" [4] Item 4", _
"Main Menu")
Case "1"
Call sub1()
Case "2"
Call sub2()
Case "3"
Call sub3()
Case "4"
Call sub4()
Case Else
WScript.Echo "You entered an invalid menu choice!"
End Select
'----------
' Clean Up
'----------
Call CleanUp
'-----------------------------------------------------------------
' Subroutines
'*****************************************************************
'---------------------
Sub CleanUp()
Set oWS = Nothing
Set oWN = Nothing
Set oFS = Nothing
WScript.Quit
End Sub
'---------------------
Sub sub1()
WScript.Echo "You selected Menu Item 1"
End Sub
'---------------------
Sub sub2()
WScript.Echo "You selected Menu Item 2"
End Sub
'---------------------
Sub sub3()
WScript.Echo "You selected Menu Item 3"
End Sub
'---------------------
Sub sub4()
WScript.Echo "You selected Menu Item 4"
End Sub
'-----------------------------------------------------------------
' Functions
'*****************************************************************
'---------------------
'***************************************

Resources