0

My script is basically the same as last time, but there are some bonus features I'm having problems with.

  1. Is there something similar to exception handling in VBScript? I've read about it and I'm not to sure and is there a way if the script gets canceled for not existing path folders, to create them and continue/restart?
  2. Is there a way how I'm able to skip (They've to be there, but it would be fancy if I could be able to skip them.) at the beginning of the script all these text messages and how is it done?

Here's the code I've got so far:

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"

How do I have to implement "On Error Resume Next"? I've done something like this right now and I'm not to sure if it's correct:

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
Community
  • 1
  • 1
NiceTry
  • 113
  • 1
  • 2
  • 8

1 Answers1

0

If I understood your question correctly, error handling should not be required for what you're trying to do. To make sure that a folder exists before doing something with it, you can simply use the FolderExists method:

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

However, if for some reason you must use error handling, it can be enabled with the statement 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.

A very simple error handling routine might look like this:

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

Error handling suppresses all runtime error messages, so you should keep it as local as possible. Having error handling enabled on a broader scope bears the risk of errors going unnoticed, causing unexpected/undesired behavior for instance due to variables being not initialized or retaining an obsolete value.

If you have several subsequent statements that could fail make sure you add error handling routines for each and clear the Err object after each statement:

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I've tried to implement some error handling, but it didn't work to well. Take a look at the question, I've edited it with some new code. Thanks – NiceTry Jan 19 '15 at 09:00
  • @NiceTry VBScript only supports inline error handling, i.e. you can't define an error handler outside the rest of your code (e.g. at the end of the script, as you have in the updated code you posted). You *must* put `On Error Resume Next` before and the error handler after the actual statement in your code. – Ansgar Wiechers Jan 19 '15 at 09:16