SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using SSIS to zip files and email the zipped files


Using SSIS to zip files and email the zipped files

Author
Message
gjohnpulle
gjohnpulle
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 25
Hi RamaA

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

Thanks
George



RamaA
RamaA
SSC-Enthusiastic
SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)

Group: General Forum Members
Points: 142 Visits: 239
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
Attachments
Process Editor.png (16 views, 17.00 KB)
kevin_nikolai
kevin_nikolai
Old Hand
Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)

Group: General Forum Members
Points: 371 Visits: 523
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
Attachments
wrar390.rar (20 views, 1.00 MB)
gcarbone-717246
gcarbone-717246
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 55
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
jmichaud
jmichaud
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 988
Sandor - what switches did you use to get the WinZip to work?
Todasdasdfd asfasf
Todasdasdfd asfasf
SSC-Enthusiastic
SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)

Group: General Forum Members
Points: 100 Visits: 109
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 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
Are you doing Data Warehouse ETL with SSIS? Don't use the slow SCD Wizard, use the Kimball Method SCD component instead.
sandor_g
sandor_g
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 66
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
jmichaud
jmichaud
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 988
Thank you!
synergyx
synergyx
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 41
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
sandor_g
sandor_g
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 66
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search