Using SSIS to zip files and email the zipped files

  • Hi RamaA

    Can you please post your mods to use WinRAR. We will be looking to use WinRAR also.

    Thanks

    George

  • Hi George,

    In Script Task click on Design Script and Add the Following:

    Imports System

    Imports System.Data

    Imports System.Math

    Imports Microsoft.SqlServer.Dts.Runtime

    Imports System.Data.OleDb

    Imports System.IO

    Public Class ScriptMain

    ' The execution engine calls this method when the task executes.

    ' To access the object model, use the Dts object. Connections, variables, events,

    ' and logging features are available as static members of the Dts class.

    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

    '

    ' To open Code and Text Editor Help, press F1.

    ' To open Object Browser, press Ctrl+Alt+J.

    Public Sub Main()

    Dim InFolder, InFileType, OutFileName, OutExecutable, OutSubject, OutFolder, OutMessage As String

    Dim FileDate, strDay, strMonth, strYear As String

    InFolder = Trim(CStr(Dts.Variables("User::InFolder").Value))

    OutFolder = Trim(CStr(Dts.Variables("User::OutFolder").Value))

    InFileType = Trim(CStr(Dts.Variables("User::InFileType").Value))

    OutFileName = "Filename.zip" 'set this to your desired zip file name

    OutExecutable = " a -m5 """ + OutFolder + OutFileName + """ """ + InFolder + "*." + InFileType + """"

    OutSubject = "Attached Zipped Files:- " + OutFileName

    OutMessage = "Add your message here"

    Dts.Variables("User::OutExecutable").Value = OutExecutable

    Dts.Variables("User::OutFileName").Value = OutFolder + OutFileName

    Dts.Variables("User::OutSubject").Value = OutSubject

    Dts.Variables("User::OutMessage").Value = OutMessage

    Dts.TaskResult = Dts.Results.Success

    End Sub

    End Class

    Please Note:

    I have removed the date stamp as it was not needed for me...

    Also use command prompt to get the desired settings you may need for WinRar.

    In Command Promt use: C:\Program Files\WinRAR>rar.exe /? to get the list of settings

    I used: OutExecutable = " a -m5 """ for best compression rate.

    ------------------------------------------------------------------------------------

    In Execute Process Task:

    SET Executable: C:\Program Files\WinRAR\Rar.exe

    AND SET WorkingDirectory: C:\Program Files\WinRAR

    Please let me know if this works for you.

    Email: RamaA@za.innovation-group.com

    Thanks.

    Regards,

    Nash

  • Hi, here is the winrar method I use. I posted the 7-zip method previously, to me both are the same, although I prefer the 7-zip method over the winrar method.

    ' test.rar (is the name of the zip file to be created).

    ' ..\OUT\ (is the folder on your hard drive where all files with *_20100104.xls exist).

    ' *_20100104.xls (is the string used in winrar's include statement, only files with _20100104.xls in all subfolders will be included in rar file).

    Copy the code below into Script Task of Visual Studio (BIDS).

    Imports System.Data.OleDb

    Imports System.IO

    Imports Microsoft.SqlServer.Dts.Runtime

    Public Class ScriptMain

    Public Sub Main()

    Dim InFolder, InFileType, OutFileName, OutExecutable, OutSubject, OutFolder, OutMessage As String

    Dim FileDate, strDay, strMonth, strYear As String

    InFolder = Trim(CStr(Dts.Variables("User::InFolder").Value))

    OutFolder = Trim(CStr(Dts.Variables("User::OutFolder").Value))

    InFileType = Trim(CStr(Dts.Variables("User::InFileType").Value))

    strDay = Right("0" + CStr(Day(Now)), 2)

    strMonth = Right("0" + CStr(Month(Now)), 2)

    strYear = CStr(Year(Now))

    FileDate = strYear + strMonth + strDay

    OutFileName = "Customername" + FileDate + ".rar"

    OutExecutable = " a -r test.rar ..\OUT\*_20100104.xls """ + OutFolder + OutFileName + """ """ + InFolder + "*." + InFileType + """"

    OutSubject = "Daily Zipfile:- " + OutFileName

    OutMessage = "Zip Successful"

    Dts.Variables("User::OutExecutable").Value = OutExecutable

    Dts.Variables("User::OutFileName").Value = OutFolder + OutFileName

    Dts.Variables("User::OutSubject").Value = OutSubject

    Dts.Variables("User::OutMessage").Value = OutMessage

    Dts.TaskResult = Dts.Results.Success

    End Sub

    End Class

  • Excellent article and very useful, but if you don't write the InFolder with the "\" final you'll receive the zip file empty, I hope this could be helpful for someone who's find the zip file empty.

    Bye

  • Sandor - what switches did you use to get the WinZip to work?

  • You can also use the one-step method of installing a custom zip task - there are several out there, the first two of them are free (source included) - no scripting or external programs necessary:

    Compress File - Compresses and decompresses files using System.IO.Compression.

    TaskUnZip - Manages compressed files (including password protected).

    Zip - compression and decompression of Zip, GZip, BZip2, Unix (rfc1950) and Tar archives.

    Zip - Zip Compression / Decompression.

    For these and other "one-step" custom tasks, visit the SSIS Community Tasks and Components[/url] project on CodePlex.

    Todd McDermid - SQL Server MVP, MCSD.Net, MCTS (SQL08BI)
    Find great SSIS extensions - most are open source - at SSIS Community Tasks and Components[/url]
    Are you doing Data Warehouse ETL with SSIS? Don't use the slow SCD Wizard, use the Kimball Method SCD component instead.

  • SSC-Enthusiastic ,

    I actually used the WInZip command line tool to do the zipping. I used a Foreach Loop Container to list all the files in a folder, with a VB script inside of it, I created a txt file containing the file names in the folder. Then I pass the filename to WinZip which reads each filename out of the text file and zipps them together. This way, it is fully flexible and automatic. To do this, in the Execute Precess Task, I used an expression for the Argument which builds from to Global Variables: @ZipFileName (also created from other variables to have the current date in it) + @ListFile (the text file containing the files in the folder needed to be zipped). So the actual Variable for the Argument looks like this: @[User::ZipFile] + " @" + @[User::ListFile]. When evaluated it returns 'c:\yourpath\yourzipfile.zip @C:\youpath\yourtextfile.txt'. You can use just this if you are making constant files all the time.

    Sandor

  • Thank you!

  • Hi Carolyn

    im having a problem with the script, in ssis 2005 it works 100%, as soon as i try it in my 2008 environment it does not find dts.variable object? any idea why this could be?

    this is the script im using but it does not recognise dts.variable

    just a note in the script task i have to selcet visual basic 2008 and not .net as it is not available but that shouldnt make a difference.

    Imports System

    Imports System.Data.OleDb

    Imports System.IO

    Imports Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel

    Imports Microsoft.SqlServer.Dts.Runtime

    Public Class ScriptMain

    Public Sub Main()

    Dim InFolder, InFileType, OutFileName, OutExecutable, OutSubject, OutFolder, OutMessage As String

    Dim FileDate, strDay, strMonth, strYear As String

    InFolder = Trim(CStr(Dts.Variables("User::InFolder").Value))

    OutFolder = Trim(CStr(Dts.Variables("User::OutFolder").Value))

    InFileType = Trim(CStr(Dts.Variables("User::InFileType").Value))

    strDay = CStr(Day(Now))

    strMonth = CStr(Month(Now))

    strYear = CStr(Year(Now))

    If Len(strDay) = 1 Then

    strMonth = "0" + strDay

    End If

    If Len(strMonth) = 1 Then

    strMonth = "0" + strMonth

    End If

    FileDate = strYear + strMonth + strDay

    OutFileName = "Archive" + FileDate + ".zip"

    OutExecutable = " a -tzip """ + OutFolder + OutFileName + """ """ + InFolder + "*." + InFileType + """"

    OutSubject = "Attached Zipped Files:- " + OutFileName

    OutMessage = "Add your message here"

    Dts.Variables("User::OutExecutable").Value = OutExecutable

    Dts.Variables("User::OutFileName").Value = OutFolder + OutFileName

    Dts.Variables("User::OutSubject").Value = OutSubject

    Dts.Variables("User::OutMessage").Value = OutMessage

    Dts.TaskResult = Dts.Results.Success

    End Sub

    End Class

  • Synergyx,

    Use this in SQL 2008:

    Imports System

    Imports System.Data

    Imports System.Math

    Imports Microsoft.SqlServer.Dts.Runtime

    <System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _

    <System.CLSCompliantAttribute(False)> _

    Partial Public Class ScriptMain

    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

    End Class

    Enum ScriptResults

    Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success

    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure

    End Enum

    Public Class ScriptMain

    Public Sub Main()

    Dim InFolder, InFileType, OutFileName, OutExecutable, OutSubject, OutFolder, OutMessage As String

    Dim FileDate, strDay, strMonth, strYear As String

    InFolder = Trim(CStr(Dts.Variables("User::InFolder").Value))

    OutFolder = Trim(CStr(Dts.Variables("User::OutFolder").Value))

    InFileType = Trim(CStr(Dts.Variables("User::InFileType").Value))

    strDay = CStr(Day(Now))

    strMonth = CStr(Month(Now))

    strYear = CStr(Year(Now))

    If Len(strDay) = 1 Then

    strMonth = "0" + strDay

    End If

    If Len(strMonth) = 1 Then

    strMonth = "0" + strMonth

    End If

    FileDate = strYear + strMonth + strDay

    OutFileName = "Archive" + FileDate + ".zip"

    OutExecutable = " a -tzip """ + OutFolder + OutFileName + """ """ + InFolder + "*." + InFileType + """"

    OutSubject = "Attached Zipped Files:- " + OutFileName

    OutMessage = "Add your message here"

    Dts.Variables("User::OutExecutable").Value = OutExecutable

    Dts.Variables("User::OutFileName").Value = OutFolder + OutFileName

    Dts.Variables("User::OutSubject").Value = OutSubject

    Dts.Variables("User::OutMessage").Value = OutMessage

    Dts.TaskResult = ScriptResults.Success

    End Sub

    End Class

  • WayneS (1/4/2010)


    clive-421796 (1/1/2010)


    How can I automate this so that I dont have to edit the Script Task every day and specify the date of previous days files to include, in this case 20091208 ( c:\temp\out\*_20091208.xls ).

    Clive,

    Set up a variable to build the day.

    Set up a variable to build the filename, based on the previous variable.

    Clive,

    Sorry for the delay, I just stumbled across your request.

    Okay, here's what to do:

    1. Add a new variable. Call it "PriorDate". Set EvaluateAsExpression to True, and set the expression to "DateAdd("dd", -1, GetDate())"

    2. Add a new variable. Call it "PriorDateYYYYMMDD". Set EvaluateAsExpresstion to True, and set the expression to:

    (DT_STR, 4, 1252) DATEPART("yyyy", @[User::PriorMonth] ) +

    RIGHT("0" + (DT_WSTR,2)MONTH(@User::[PriorDate]), 2) +

    RIGHT("0" + (DT_WSTR,2)DAY(@User::[PriorDate]), 2)

    3. Add a new variable. Call it "FileName". Set EvaluateAsExpression to True, and set the expression to:

    "C:\temp\out\x\*_" + @User::[PriorDateYYYYMMDD] + ".xls".

    4. In the Script task, Add the PriorDateYYYYMMDD variable as a ReadOnlyVariable. You can now use that variable inside your script task.

    HTH,

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Hi WayneS,

    Thanks.

    Since the date will always be previous date, I added everything directly to the script task in SSIS package. Will create a copy of current SSIS package and implement your method for comparison.

  • Very helpful, thanks Carolyn.

  • Hi,

    Im getting an error requesting me declare dts in the scriptask. Any idea why this is happening?

    Thanks,

    T

  • really very good and helpful....did my job... 🙂

Viewing 15 posts - 61 through 75 (of 81 total)

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