' Software- und Organisations-Service GmbH, 2005, www.sos-berlin.com
' Andreas Püschel, 2005-05-30, andreas.pueschel@sos-berlin.com
'
' This script simply moves files from an input directory to an output directory
' and demonstrates the usage of the Job Scheduler API methods for logging and error handling:


Option Explicit
On Error Goto 0

Dim i, fso, directories, directoryPaths, directory, files, inputFile, inputFilename, outputFilename

Set fso = CreateObject("Scripting.FileSystemObject")

directories = spooler_task.changed_directories
directoryPaths = split(directories, ";")

For i=LBound(directoryPaths) To UBound(directoryPaths)
  If (Len(directoryPaths(i)) > 0) Then
    Set directory = fso.GetFolder(directoryPaths(i))
    Set files = directory.Files
    For Each inputFile in files
      inputFilename = inputFile.Path
      outputFilename = spooler_task.params.var("output_directory") & "/" & inputFile.Name

      If (fso.FileExists(outputFilename)) Then
         On Error Resume Next
          Call fso.DeleteFile(outputFilename)
          If (err.number <> 0) Then
            spooler_log.warn("output file probably in use, trying later: " & outputFilename)
          End If
        On Error Goto 0
      End If

      If (Not fso.FileExists(outputFilename)) Then
        On Error Resume Next
          Call fso.MoveFile(inputFilename, outputFilename)
          If (err.number <> 0) Then
            spooler_log.warn("could not rename input file [" & inputFilename & "] " & _
                           "to output file [" & outputFilename & "]")
          Else
            spooler_log.info("input file [" & inputFilename & "] " & _
                           "successfully moved to output file [" & outputFilename & "]")
          End If
        On Error Goto 0
      End If
    Next
  End If
Next