异常处理和跳过短信

时间:2015-01-18 15:54:03

标签: vbscript

我的脚本与last time基本相同,但有一些额外的功能我遇到了问题。

  1. VBScript中有类似于异常处理的东西吗?我已经阅读了它并且我不确定是否有办法取消现有路径文件夹的脚本,创建它们并继续/重新启动?
  2. 有没有办法让我能够跳过(他们会在那里,但如果我能够跳过它们会很奇怪。)在脚本的开头所有这些文字消息以及如何完成?
  3. 这是我到目前为止所获得的代码:

    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Function Pad(s)
      Pad = Right("00" & s, 2)
    End Function
    
    Sub CopyFiles(fldr, dst)
      'Copy all files from fldr to destination folder and append the date (in ISO
      'format) to the name. Overwrite existing files.
      For Each f In fldr.Files
        created = Year(f.DateCreated) & "-" & Pad(Month(f.DateCreated)) & "-" & _
                  Pad(Day(f.DateCreated)) & "_" & Pad(Hour(f.DateCreated)) & _
                  Pad(Minute(f.DateCreated)) & Pad(Second(f.DateCreated))
        newname = fso.GetBaseName(f) & "_" & created & "." & fso.GetExtensionName(f)
        WScript.Echo "Aktuelles File, welches gerade kopiert wird: " & newname
        f.Copy fso.BuildPath(dst, newname), True
      Next
    
      'Recurse into subfolders.
      For Each sf In fldr.SubFolders
        CopyFiles sf, dst
      Next
    End Sub
    
    CopyFiles fso.GetFolder("C:\test"), "C:\test1"
    

    我如何实施" On Error Resume Next"? 我现在做了类似的事情,我不确定它是否正确:

    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FolderExists("C:\test") Then
    On Error Goto 0
    Dim StartFolder, TargetFolder
    StartFolder = "C:\test"
    TargetFolder = "C:\test1"
    
    Function Pad(s)
      Pad = Right("00" & s, 2)
    End Function
    
    Sub CopyFiles(fldr, dst)
      'Copy all files from fldr to destination folder and append the date (in ISO
      'format) to the name. Overwrite existing files.
      For Each f In fldr.Files
        created = Year(f.DateCreated) & "-" & Pad(Month(f.DateCreated)) & "-" & _
                  Pad(Day(f.DateCreated)) & "_" & Pad(Hour(f.DateCreated)) & Pad(Minute(f.DateCreated)) & Pad(Second(f.DateCreated))
        newname = fso.GetBaseName(f) & "_" & created & "." & fso.GetExtensionName(f)
        If UCase(FSO.GetExtensionName(f.name)) = "JPG" Then
            f.Copy fso.BuildPath(dst, newname), True        
            WScript.Echo "Ich kopiere: " & StartFolder & "\" & f.name & " nach " & TargetFolder & "\" & newname
        End If  
      Next
    
      'Recurse into subfolders.
      For Each sf In fldr.SubFolders
        CopyFiles sf, dst
      Next
    
    End Sub
    
    CopyFiles fso.GetFolder("C:\test"), "C:\test1"
    End If
    On Error Resume Next
     f.Copy fso.BuildPath(dst, newname), True
     If Err Then
      WScript.Echo Err.Description & " [0x" & Hex(Err.Number) & "]"
     End If
    On Error Goto 0
    

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么您尝试做的事情不应该是错误处理。要在使用文件夹之前确保文件夹存在,您只需使用FolderExists方法:

If fso.FolderExists("C:\some\folder") Then
  'do stuff
End If

但是,如果由于某种原因必须使用错误处理,则可以使用语句On Error Resume Next启用它,并使用语句On Error Goto 0禁用它。启用错误处理时,您可以通过检查Err对象的状态来检测错误。

一个非常简单的错误处理例程可能如下所示:

On Error Resume Next
f.Copy fso.BuildPath(dst, newname), True
If Err Then
  WScript.Echo Err.Description & " [0x" & Hex(Err.Number) & "]"
End If
On Error Goto 0

错误处理会抑制所有运行时错误消息,因此您应该将其保留为本地错误消息。在更广泛的范围内启用错误处理会带来错误未被注意的风险,从而导致意外/不期望的行为,例如由于变量未初始化或保留过时的值。

如果您有几个可能失败的后续语句,请确保为每个语句添加错误处理例程,并在每个语句后清除Err对象:

On Error Resume Next
Set wmi = GetObject("winmgmts://./root/cimv2")
If Err Then
  WScript.Echo Err.Description & " [0x" & Hex(Err.Number) & "]"
End If
Err.Clear
Set proc = wmi.ExecQuery("SELECT * FROM Win32_Process")
If Err Then
  WScript.Echo Err.Description & " [0x" & Hex(Err.Number) & "]"
End If
Err.Clear
'...
On Error Goto 0