Wednesday, January 11, 2012

Check for file existance, copy or skip using script taks

A recent question about how to check for file existance, skip or copy using script taks. from 2 different locations.

 Here I'll ge ta list of files that needs to be copied from C:\Media, check if it exists in U:Media or not and act accordingly.


Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO
<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
 Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
 Enum ScriptResults
  Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
  Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
 End Enum

    Public Sub Main()
        Dim FileToCopy As String
        Dim NewCopy As String
        Dim di As New IO.DirectoryInfo("C:\Media")
        Dim fi As IO.FileInfo() = di.GetFiles()
        Dim f As IO.FileInfo

        For Each f In fi
            FileToCopy = "C:\Media\" + f.ToString
            NewCopy = "U:\Media\" + f.ToString
            If File.Exists(NewCopy) Then
                MsgBox(NewCopy + "File Exists")
            Else
                System.IO.File.Copy(FileToCopy, NewCopy)
                MsgBox(NewCopy + "File Copied")
            End If
        Next
        Dts.TaskResult = ScriptResults.Success
    End Sub
End Class



Hope that helps.... 

No comments:

Post a Comment