0

I wan't to upload periodically a file to samba share. My script works perfectly, but it crashes if samba share is not accessible (i.e. server or network is down). It is possible to run my vbs script silently ( to ignore errors ) ?

this is my piece of code:

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

I tried to use "On Error resume next" statement, but it crashes anyway.

1 Answer 1

1

I dont think having an on error resume next statement is the best option, and i believe this is bad coding practice. I would consider doing something like this, which will improve coding and stop repeating code.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.