none
Create VBS file by useing batch file RRS feed

  • Question

  • Hi all

    I'm still beginner in batch files coding ,so i'm asking for help
    I want to create VBS file to display specific message then delete it by using batch file
    but it seems like something wrong in my batch commands and here is my batch file script below:

    @echo off
    echo x=msgbox("The Reverse Copy has been Applied Successfully" & vbCrLf & "For more Details:_"& vbCrLf & "you can open 'Error Report file'",0, "Succeeded Message ")> a.vbs

    a.vbs
    del a.vbs
    exit

    When i tried to write the below command

     x=msgbox("The Reverse Copy has been Applied Successfully" & vbCrLf & "For more Details:_"& vbCrLf & "you can open 'Error Report file'",0, "Succeeded Message ")

    in separated VBS file it works properly but when i tried to insert it into VBS file using batch it give nothing in my vbs created file ( i mean it will create the VBS file as blank file)

    Monday, November 16, 2020 11:48 AM

Answers

  • You have to escape the comercial e "&" character with escape character "^" since "&" has a special meaning in batch, it means that you want to use more than 1 command in one line.

    So if you want the commercial e to be seen as simple text instead of concatenating commands you have to use: ^&

    @echo off
    echo x=msgbox("The Reverse Copy has been Applied Successfully" ^& vbCrLf ^& "For more Details:_"^& vbCrLf ^& "you can open 'Error Report file'",0, "Succeeded Message ")> a.vbs
    
    a.vbs
    del a.vbs
    exit


    R. Bohner

    • Marked as answer by Emotop Monday, November 16, 2020 1:30 PM
    • Unmarked as answer by Emotop Monday, November 16, 2020 1:31 PM
    • Marked as answer by Emotop Monday, November 16, 2020 1:31 PM
    Monday, November 16, 2020 12:05 PM
  • Don't use either a .bat or a .vbs. Use Powershell instead. Convert the .bat file to a .ps1. You will have a learning curve but in the long run the power and flexibility of Powershell will allow you to do so much more.  

    https://docs.microsoft.com/en-us/powershell/scripting/learn/ps101/01-getting-started?view=powershell-5.1

    https://michlstechblog.info/blog/powershell-show-a-messagebox/

    https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window

    • Marked as answer by Emotop Monday, November 16, 2020 2:53 PM
    Monday, November 16, 2020 2:33 PM
  • You can use a Combination of 2 batch files.

    Let's consider the following scenario:

    You have this 2 batch files on your desktop:

    Starter.bat: Is going to create a vbs called runHidden that is going to run your second batch (batch.bat) in hidden mode.

    batch.bat: Is going to show only the message: The Reverse Copy has been Applied Successfully, without any command window behind it...

    Both batches are on the deskotp...

    Code for starter.bat:

    @echo off
    echo CreateObject("Wscript.Shell").Run "cmd /c ""C:\Users\ricar\Desktop\batch.bat""",0,false>"%temp%\runHidden.vbs"
    %temp%\runHidden.vbs

    Code for batch.bat:

    @echo off
    echo x=msgbox("The Reverse Copy has been Applied Successfully" ^& vbCrLf ^& "For more Details:_"^& vbCrLf ^& "you can open 'Error Report file'",0, "Succeeded Message ")> a.vbs
    
    a.vbs
    del a.vbs
    exit

    A command window only briefly pops up for the starter.bat but not for the batch.bat:

    ]

    Another method to run batch files in hidden mode is to use Task Scheduler, by using the system account or the option: Run task if user is conneted or not....


    R. Bohner

    • Marked as answer by Emotop Tuesday, November 17, 2020 11:25 AM
    Monday, November 16, 2020 2:59 PM

All replies

  • You have to escape the comercial e "&" character with escape character "^" since "&" has a special meaning in batch, it means that you want to use more than 1 command in one line.

    So if you want the commercial e to be seen as simple text instead of concatenating commands you have to use: ^&

    @echo off
    echo x=msgbox("The Reverse Copy has been Applied Successfully" ^& vbCrLf ^& "For more Details:_"^& vbCrLf ^& "you can open 'Error Report file'",0, "Succeeded Message ")> a.vbs
    
    a.vbs
    del a.vbs
    exit


    R. Bohner

    • Marked as answer by Emotop Monday, November 16, 2020 1:30 PM
    • Unmarked as answer by Emotop Monday, November 16, 2020 1:31 PM
    • Marked as answer by Emotop Monday, November 16, 2020 1:31 PM
    Monday, November 16, 2020 12:05 PM
  • Many Thanks R. Bohner for helping me as it works properly after your advice and your edit . but can we remove or disappear the CMD window which appeared behind the popup message.
    Monday, November 16, 2020 1:30 PM
  • Don't use either a .bat or a .vbs. Use Powershell instead. Convert the .bat file to a .ps1. You will have a learning curve but in the long run the power and flexibility of Powershell will allow you to do so much more.  

    https://docs.microsoft.com/en-us/powershell/scripting/learn/ps101/01-getting-started?view=powershell-5.1

    https://michlstechblog.info/blog/powershell-show-a-messagebox/

    https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window

    • Marked as answer by Emotop Monday, November 16, 2020 2:53 PM
    Monday, November 16, 2020 2:33 PM
  • You can use a Combination of 2 batch files.

    Let's consider the following scenario:

    You have this 2 batch files on your desktop:

    Starter.bat: Is going to create a vbs called runHidden that is going to run your second batch (batch.bat) in hidden mode.

    batch.bat: Is going to show only the message: The Reverse Copy has been Applied Successfully, without any command window behind it...

    Both batches are on the deskotp...

    Code for starter.bat:

    @echo off
    echo CreateObject("Wscript.Shell").Run "cmd /c ""C:\Users\ricar\Desktop\batch.bat""",0,false>"%temp%\runHidden.vbs"
    %temp%\runHidden.vbs

    Code for batch.bat:

    @echo off
    echo x=msgbox("The Reverse Copy has been Applied Successfully" ^& vbCrLf ^& "For more Details:_"^& vbCrLf ^& "you can open 'Error Report file'",0, "Succeeded Message ")> a.vbs
    
    a.vbs
    del a.vbs
    exit

    A command window only briefly pops up for the starter.bat but not for the batch.bat:

    ]

    Another method to run batch files in hidden mode is to use Task Scheduler, by using the system account or the option: Run task if user is conneted or not....


    R. Bohner

    • Marked as answer by Emotop Tuesday, November 17, 2020 11:25 AM
    Monday, November 16, 2020 2:59 PM
  • Thank you R. Bohner for your extra help, which helped me a lot
    I applied everything that I explained to me and the message was actually done the way I needed

    Can you help me find an explanation for a specific person or Detailed course in batch files completely and in detail so that I become a professional and not just a beginner.

    Tuesday, November 17, 2020 11:26 AM
  • Like the other person replied, if you are starting I would rather recommend powershell since it is the successor of batch and a more recent scripting language. I'm starting myself on powershell only done a couple of lessons. But if you want batch I learned a lot from the John Hammond vids:

    https://www.youtube.com/watch?v=JbAyxKjUPs0&list=PL69BE3BF7D0BB69C4

    There are 93 batch lessons of 5 min each. Also by analysing code form other people at stackoverflow.com or superuser.com


    R. Bohner


    Tuesday, November 17, 2020 11:33 AM