0

I need to create a .vbs or .bat file to be used in a task of Windows 10 Task Scheduler (I know it works for .vbs files, dunno if with .bat files is the same).

What I need is simple: I need to see a message box at Windows startup/logon asking me if I wish to run a determined app that I have installed in my system with two options: OK / Cancel

Of course with OK the app is then run, with Cancel it exits with no extra interaction.

Can someone have the code to fill in the .vbs file?

I have a .vbs starting at Windows logon that creates a message box with a simple info I need to see if a determined event happens, so I was thinking that maybe .vbs files can also show me not only message boxes but also boxes with buttons. Am I right?

Regarding Task Scheduler, I will set to load the .vbs file at Windows log on with a few secs of delay.

Thank you all.

2
  • Why not just add it to startup and not task scheduler? Commented Oct 26, 2020 at 16:45
  • You can get inspired and modify to your aim with this vbscript Auto-Lock_On_Idle_TimeOut.vbs because it can load at startup ; you have just to add your MsgBox with question Yes/Or/No; and remove the Powershell script section !
    – Hackoo
    Commented Oct 27, 2020 at 11:08

3 Answers 3

0

Bellow is a vbscript to do what I am understanding that you are asking for. I will say it is generic and I would not just copy and paste it.

First Step is display the message box and store the return value. The return value will be set based off the button pressed. Once we capture the return value we can use an if Statement to check which button was pressed.

Inside of the ok 'rt = 1' branch we create a shell object and run a program, in this case FileZilla.

I have left the code for the cancel button in to show a cancel message but have commented it out and it can be removed.

Beyond that if you want to consolidate scripts I think it is up to you. I would prefer to keep them separate mainly to simplify trouble shooting, changing triggers, and separating the concerns of the script. That way if you want to change the triggers on one you dont have to change the triggers on the other. Also if you want to remove one script in the future you dont have to break out / recreate a new one etc.

' Create the messagebox with OK / Cancel
rt = MsgBox ("Welcome", 1)

' Check Return type 1 == ok 2 == cancel
if rt = 1 then
    ' Get Shell to start application
    Set shell = WScript.CreateObject("WScript.Shell")

    ' Run the desired application
    shell.Run("""C:\Program Files (x86)\FileZilla FTP Client\Filezilla.exe""")

    ' Clear the shell
    Set shell = nothing
'elseif rt = 2 then
    ' As an example cancel Messagebox when Cancel is pressed
'    MsgBox ("Cancel")
end if

' Clear return value
rt = nothing
2
  • Thank you!! But "rt = nothing" at the end was giving me error, and I was able to fix it with "rt = 0" or "rt = null" (not sure what is better - it works with both). Do you suggest to but something different?? Commented Oct 26, 2020 at 23:32
  • Im am sure that is fine. I am not an expert in vbscript etc. I do mainly c++ and had to google some syntax. That should be mainly making sure the variable does not have things left over for reuse in a script.
    – Ed B
    Commented Oct 28, 2020 at 13:55
0

Here is a tested and modified vbscript that ask you a question if you want to start and play a radio station or not !


Option Explicit
    Dim VbsPath,ShortcutName
    VbsPath = Wscript.ScriptFullName
    ShortcutName = "Play my Radio"
    Call Shortcut(VbsPath,ShortcutName)
    Call Execute_My_Program()
'---------------------------------------------------------------------------------------------------------------
Sub Shortcut(PathApplication,ShortcutName)
    Dim objShell,StartFolder,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If ShortcutName = "" Then
        ShortcutName = MyTab(UBound(MyTab))
    End if
    StartFolder = objShell.SpecialFolders("Startup")
    Set objShortCut = objShell.CreateShortcut(StartFolder & "\" & ShortcutName & ".lnk")
    objShortCut.TargetPath = DblQuote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,138"
    objShortCut.Save
End Sub
'---------------------------------------------------------------------------------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'---------------------------------------------------------------------------------------------------------------
Sub Execute_My_Program()
Dim Title,ws,Question,PathProgram
Title = "Execute Program"
Set ws = CreateObject("wscript.shell")
'change the path of your program
'PathProgram = "C:\Program Files\Internet Explorer\iexplore.exe"
'Question = Msgbox("Do you want to start Internet Explorer ?",VbYesNO + VbQuestion, Title)
Question = Msgbox("Do you want to start Euro Dance Radio ?",VbYesNO + VbQuestion, Title)
If Question = VbYes Then
     'ws.run DblQuote(PathProgram)
     Call Play("http://94.23.221.158:9197/stream")
Else
    ws.run "Taskkill /F /IM wscript.exe",0,True
    Wscript.Quit
End If
End Sub
'---------------------------------------------------------------------------------------------------------------
Sub Play(URL)
    Dim ws,fso,f,Temp,PathFile
    Set ws = CreateObject("wscript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Temp = WS.ExpandEnvironmentStrings("%Temp%")
    PathFile = Temp & "\Radio_EuroDance90.vbs"
    Set f = fso.OpenTextFile(PathFile,2,True)
    f.Writeline     "Call Play(" & chr(34) & URL & chr(34) & ")"
    f.Writeline "Sub Play(URL)"
    f.Writeline "Set Sound = CreateObject(""WMPlayer.OCX"")"
    f.Writeline "Sound.URL = URL"
    f.Writeline "Sound.settings.volume = 100"
    f.Writeline "Sound.Controls.play" 
    f.Writeline "do while Sound.currentmedia.duration = 0"
    f.Writeline     "wscript.sleep 100"
    f.Writeline "loop"
    f.Writeline "wscript.sleep (int(Sound.currentmedia.duration)+1)*1000" 
    f.Writeline "End Sub"
    f.close
    ws.run PathFile
End Sub
'---------------------------------------------------------------------------------------------------------------
0

Thank you to everybody. I have made my own vbs script from your help. I have made a mix between all your hints. Here is the code:

' --------------------------
' Create the messagebox with Yes / No
Dim Title,App,Question
Title = "Warning"

' First Question is asked:
Question = MsgBox ("Do you want to load the desired app?", VbYesNO + VbQuestion, Title)
 
' Check if No is pressed at first Question and quit script
if Question = VbNo then
    Call Fin()
end if

' As first question is Yes, Second Question is asked:
Question = Msgbox("Are you sure??",VbYesNO + VbQuestion, Title)

' Check if Yes is pressed at second Question:
if Question = VbYes then

    ' Get Shell to start application
    Set App = WScript.CreateObject("WScript.Shell")
    ' Run the desired application
    App.Run("""C:\Program Files (x86)\xxx\xxx.exe""")

    ' Clear the shell
    'Set App = nothing
    App.run "Taskkill /F /IM wscript.exe",0,True
    Call Fin()
else
    Call Fin()
end if

' Final quit:
Sub Fin()
' Clear return value and Quit
Question = null
Wscript.Quit
End Sub
' --------------------------

If you see up here in the code in bolt I have choose to use the App.run "Taskkill /F /IM wscript.exe",0,True solution instead than the Set App = nothing (commented in code).

Is this correct? Thank you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .