Script task help

  • I am trayng to create a package on SSIS where the last step is to delete files older than 7 days.

    I found an article here with the code to create it (http://qa.sqlservercentral.com/articles/Administering/usingvbscripttoautomatetasks/1171/)

    But I am new to SSIS and also to VBS and do not know how to modify it so it can be used on the script task. can any one help ??

    Here is the script

    Option Explicit

    on error resume next

    Dim oFSO

    Dim sDirectoryPath

    Dim oFolder

    Dim oFileCollection

    Dim oFile

    Dim iDaysOld

    'Customize values here to fit your needs

    iDaysOld = 7

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    sDirectoryPath = "C:\test"

    set oFolder = oFSO.GetFolder(sDirectoryPath)

    set oFileCollection = oFolder.Files

    'Walk through each file in this folder collection.

    'If it is older than (7) days, then delete it.

    For each oFile in oFileCollection

    If oFile.DateLastModified < (Date() - iDaysOld) Then

    oFile.Delete(True)

    End If

    Next

    'Clean up

    Set oFSO = Nothing

    Set oFolder = Nothing

    Set oFileCollection = Nothing

    Set oFile = Nothing

  • If you're using the Script Task in SSIS you would use VB.NET instead of VBScript. The steps are about the same as the script you posted though, just different syntax and functions.

    Example:

    'VB.NET in Script Task

    Public Sub Main()

    ' Add your code here

    Dim FileList As String()

    FileList = System.IO.Directory.GetFiles("C:\test")

    Dim item As String

    For Each item In FileList

    If DateDiff(DateInterval.Day, System.IO.File.GetCreationTime(item).Date, Today()) > 7 Then

    System.IO.File.Delete(item)

    End If

    Next

    Dts.TaskResult = Dts.Results.Success

    End Sub

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply