4

I am making batch scripts for various purposes, and want to make it so that after a command is completed, a message dialog is displayed that will declare the results.

For instance, if I make a script delete a specific object, I would like it to display a user-defined dialog. I would prefer to use this format for the window title and text values:

x=msgbox("%MESSAGE%" ,0, "%TITLE%")

How can I add this (above) to my script to be displayed after the task (below) is complete?

rmdir "%TARGETPATH%
  • I'll give you a hint to start <command> &&: ss64.com/nt/syntax-redirection.html – Pimp Juice IT Aug 31 '17 at 23:05
  • So if I'm interperating this correctly, I need to set the task as commandA, and the result message as commandB? If that is correct, I have no idea how to dictate this in the script. I think I'll just come back to this one when I can properly program... – Mr. Mendelli Aug 31 '17 at 23:28
  • Thank you, I was planning on figuring this out using your reference but honestly wouldn't have got even close to your answer for some time. – Mr. Mendelli Sep 4 '17 at 7:47
4

You can use command redirection with && or & to execute a proceeding command after the first command completes. You can set it to execute the proceeding command after the first command was successful, or directly after the first command executes regardless of the first command result.

The trick is to have the batch script put the VB msgBox function and applicable values it uses into a temp script that will later be executed via wscript.

This should all work out of the box without installing any third party tools so it's Windows native.


Batch Script

Note: You will need to use the syntax of SET x=msgbox "%TITLE%",0,"%MESSAGE%" rather than what you have for this to work but that's a minor adjustment only.

@ECHO ON

:Routine1
SET TARGETPATH=C:\Path\Folder
SET COMMAND=rmdir "%TARGETPATH%
SET TITLE=This is my message title
SET MESSAGE=This is my message body
SET tmpmsgbox=%temp%\~tmpmsgbox.vbs
SET x=msgbox "%MESSAGE%",0,"%TITLE%"
ECHO %x%>"%tmpmsgbox%"
%COMMAND% && WSCRIPT "%tmpmsgbox%"
EXIT

Redirection

   commandA &  commandB      Run commandA and then run commandB
   commandA && commandB      Run commandA, if it succeeds then run commandB

Further Resources

  • Your answer worked exceptionally, but I must note that if the temp vbs message method is used, the message title and text are reversed for some reason. I fixed it simply by switching the variables. – Mr. Mendelli Sep 1 '17 at 1:13
  • 1
    @Mr.Mendelli Ah, I copied that from your question and left those in the same order and just didn't notice that. I've updated the answer and switched those in the example script as well. I'm glad to hear it worked well for you. – Pimp Juice IT Sep 1 '17 at 1:15
  • No problem, I updated my question in accordance as well. Great work as usual. – Mr. Mendelli Sep 1 '17 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.