异常处理和跳过短信

exception handling and skip text messages

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

  1. 在VBScript中有类似异常处理的东西吗?我已经读过它,但我不确定,如果脚本因不存在的路径文件夹而被取消,是否有办法创建它们并 continue/restart?
  2. 有没有办法让我能够跳过(它们必须在那里,但如果我能够跳过它们就更好了。)在脚本的开头所有这些短信和怎么做到的?

这是我目前得到的代码:

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

如果我对你的问题的理解正确,那么你正在尝试做的事情应该不需要错误处理。要在对文件夹进行操作之前确保该文件夹存在,您可以简单地使用 FolderExists 方法:

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

但是,如果出于某种原因必须使用错误处理,可以使用语句 On Error Resume Next and disabled with the statement On Error Goto 0. While error handling is enabled you can detect errors by checking the state of the Err object 来启用它。

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

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

错误处理会抑制所有运行时错误消息,因此您应尽可能将其保留在本地。在更广泛的范围内启用错误处理会承担错误被忽视的风险,例如由于变量未初始化或保留过时值而导致 unexpected/undesired 行为。

如果您有多个后续语句可能会失败,请确保为每个语句添加错误处理例程并在每个语句后清除 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