【问题标题】:How can i run the script without showing me errors如何在不显示错误的情况下运行脚本
【发布时间】:2019-12-30 08:02:06
【问题描述】:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
dim filesys
oShell.Run "taskkill /F /IM mysqld.exe", , True
Dim WShell
Set fso = CreateObject("Scripting.FileSystemObject")
file = ("C:\xampp\mysql\bin\mysqld.exe")
fso.DeleteFile file
Set WShell = Nothing 

我获得权限被拒绝以用户权限运行脚本,我想要的是即使我获得权限被拒绝,脚本也不会显示该错误。

【问题讨论】:

标签: vbscript


【解决方案1】:

参考On Error - statement of language VBScript

如果您不使用On Error Resume Next 语句,那么发生的任何运行时错误都是致命的; 即显示错误消息并停止执行。

On Error Resume Next 导致继续执行紧跟在导致运行时错误的语句之后的语句,或者紧跟在最近调用包含On Error Resume Next 语句的过程之后的语句。

尽管出现运行时错误,这仍允许继续执行。然后,您可以在过程中内联构建错误处理例程。

如果调用另一个过程,On Error Resume Next 语句将变为非活动状态,因此如果您希望在该例程中进行内联错误处理,则应在每个被调用例程中执行 On Error Resume Next 语句。

On Error Resume Next
Dim fso,oShell,file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
oShell.Run "Taskkill /F /IM mysqld.exe",0,True
file = "C:\xampp\mysql\bin\mysqld.exe"
If fso.FileExists(file) Then
    fso.DeleteFile file
End If
Set oShell = Nothing 
Set fso = Nothing

【讨论】:

    【解决方案2】:

    您可以在错误处理中添加更多内容:

    Dim oShell
    Set oShell = CreateObject("WScript.Shell")
    Dim filesys
    oShell.Run "taskkill /F /IM mysqld.exe", , True
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = ("C:\xampp\mysql\bin\mysqld.exe")
    On Error Resume Next
    fso.DeleteFile file
    On Error Goto 0
    Set fso = Nothing 
    

    您可以随时使用On Error Goto 0 语句禁用错误处理。 还有一个Err 对象。 Err.Raise ErrorCode 使用错误代码引发错误。 Err.Number 给出错误代码,Err.Description 给出错误详情。

    【讨论】:

      猜你喜欢
      • 2010-12-20
      • 2022-06-13
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      相关资源
      最近更新 更多