错误在vbs中恢复

时间:2014-06-12 14:43:29

标签: vbscript try-catch

我不想定期将文件上传到samba共享。我的脚本运行正常,但如果无法访问samba共享(即服务器或网络已关闭),它会崩溃。可以静默运行我的vbs脚本(忽略错误)?

这是我的代码:

while True
On Error resume next
  NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password
  Set Directory = FSO.GetFolder(ServerShare)

WScript.Sleep 2000

  folderName =  "\\10.10.10.10\smb\" & strComputerName

  If Not FSO.FolderExists(folderName) Then
    FSO.CreateFolder folderName
  End If

  folderName =  "\\10.10.10.10\smb\" & strComputerName & "\" & Year(now)
  If Not FSO.FolderExists(folderName) Then
    FSO.CreateFolder folderName
  End If

  folderName =  "\\10.10.10.10\smb\" & strComputerName & "\" & Year(now) & "\" &  Month(now)
  If Not FSO.FolderExists(folderName) Then
    FSO.CreateFolder folderName
  End If

  folderName =  "\\10.10.10.10\smb\" & strComputerName & "\" & Year(now) & "\" &  Month(now) & "\" & Day(now)
  If Not FSO.FolderExists(folderName) Then
    FSO.CreateFolder folderName
  End If

  DestinationFile = folderName & "\" & hour(now) & "_" & minute(now) & "_" &second(now) &  ".png"
  fso.CopyFile SourceFile & "\1.tmp", DestinationFile

WScript.Sleep 2000

fso.DeleteFile(SourceFile & "\1.tmp")

WScript.Sleep 2000
wend

我尝试使用“On Error resume next”语句,但无论如何它都会崩溃。

1 个答案:

答案 0 :(得分:1)

我不认为拥有on error resume next语句是最好的选择,我相信这是糟糕的编码习惯。我会考虑做这样的事情,这将改善编码并停止重复代码。

NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password
Set Directory = FSO.GetFolder(ServerShare)

WScript.Sleep 2000

folderName =  "\\10.10.10.10\smb\" & strComputerName
Call Check_Folder(folderName)

folderName =  "\\10.10.10.10\smb\" & strComputerName & "\" & Year(now)
Call Check_Folder(folderName)

'add in the rest of the foldernames and call check_folder lines

DestinationFile = folderName & "\" & hour(now) & "_" & minute(now) & "_" &second(now) &  ".png"
  fso.CopyFile SourceFile & "\1.tmp", DestinationFile

WScript.Sleep 2000

fso.DeleteFile(SourceFile & "\1.tmp")

WScript.Sleep 2000



'Sub to increase code reuse
Sub Check_Folder(folderName)

'Begin error checking
On error resume next
If Not FSO.FolderExists(folderName) Then
    FSO.CreateFolder folderName
End If

If err.number <> 0 Then
   'There is an error here, do something or nothing
End If

'Clear error
On error go to 0

End Sub