我需要创建一个 .vbs 或 .bat 文件以用于 Windows 10 任务计划程序的任务(我知道它适用于 .vbs 文件,不知道 .bat 文件是否相同)。
我需要的很简单: 我需要在 Windows 启动/登录时看到一个消息框,询问我是否要运行已安装在系统中的确定应用程序,并提供两个选项:确定/取消
当然,单击“确定”后应用程序就会运行,单击“取消”后应用程序就会退出,无需任何额外的交互。
有人可以有填写 .vbs 文件的代码吗?
我有一个 .vbs 文件,它在 Windows 登录时启动,创建一个消息框,其中包含一些我需要查看的简单信息,以确定是否发生了确定的事件,所以我在想,也许 .vbs 文件不仅可以显示消息框,还可以显示带有按钮的框。我说得对吗?
关于任务计划程序,我将设置在 Windows 登录时加载 .vbs 文件,并延迟几秒钟。
谢谢你们。
答案1
下面是一个 vbscript,据我了解,它可以完成您要求的任务。我会说它是通用的,我不会直接复制粘贴它。
第一步是显示消息框并存储返回值。返回值将根据按下的按钮设置。一旦我们捕获了返回值,我们就可以使用 if 语句来检查按下了哪个按钮。
在 ok 'rt = 1' 分支内,我们创建一个 shell 对象并运行一个程序,在本例中为 FileZilla。
我保留了取消按钮的代码以显示取消消息但已将其注释掉并且可以删除。
除此之外,如果您想合并脚本,我认为这取决于您。我更愿意将它们分开,主要是为了简化故障排除、更改触发器和分离脚本的关注点。这样,如果您想更改一个脚本上的触发器,则不必更改另一个脚本上的触发器。此外,如果您将来想删除一个脚本,您不必拆分/重新创建一个新脚本等。
' 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
这是一个经过测试和修改的 vbscript,它会询问您是否要启动并播放广播电台!
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
'---------------------------------------------------------------------------------------------------------------
答案3
谢谢大家。我已经在你们的帮助下制作了自己的 vbs 脚本。我结合了你们的所有提示。以下是代码:
' --------------------------
' 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
' --------------------------
如果你看到这里的代码在螺栓中我选择使用App.run "Taskkill /F /IM wscript.exe",0,True解决方案,而不是设置应用程序=无(代码中注释)。
这样对吗?谢谢。