Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««45678»»

Using SSIS to zip files and email the zipped files Expand / Collapse
Author
Message
Posted Tuesday, January 5, 2010 4:51 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, November 16, 2013 2:51 AM
Points: 4, Visits: 17

Hi RamaA

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

Thanks
George



Post #842500
Posted Wednesday, January 6, 2010 3:49 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, March 22, 2013 2:25 AM
Points: 102, Visits: 151
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


  Post Attachments 
Process Editor.png (10 views, 17.46 KB)
Post #842656
Posted Wednesday, January 6, 2010 11:35 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 29, 2014 3:31 PM
Points: 161, Visits: 461
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


  Post Attachments 
wrar390.rar (11 views, 1.28 MB)
Post #843066
Posted Thursday, January 7, 2010 5:00 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:22 AM
Points: 19, Visits: 54
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
Post #843440
Posted Thursday, January 7, 2010 2:38 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 8:25 AM
Points: 244, Visits: 988
Sandor - what switches did you use to get the WinZip to work?
Post #843897
Posted Thursday, January 7, 2010 11:27 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 22, 2010 9:44 AM
Points: 36, 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.
Post #844117
Posted Friday, January 8, 2010 6:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 24, 2012 3:15 PM
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
Post #844239
Posted Friday, January 8, 2010 6:39 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 8:25 AM
Points: 244, Visits: 988
Thank you!
Post #844246
Posted Friday, January 15, 2010 5:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, January 2, 2011 11:02 PM
Points: 2, Visits: 35
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
Post #848192
Posted Friday, January 15, 2010 6:05 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 24, 2012 3:15 PM
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
Post #848197
« Prev Topic | Next Topic »

Add to briefcase «««45678»»

Permissions Expand / Collapse