Using SSIS to zip files and email the zipped files

 

Using SSIS to zip files and email the zipped files

By Carolyn Richardson, 2008/09/22

 

This article makes use of global variables throughout and shows how they can be used in SSIS, you can adapt these to make a package that can be made as flexible as you want. The package described here zips all files in a folder to a single archive zip file.

To zip files this package uses 7-zip available from http://www.7-zip.org, it has some limitations on file sizes, but its Open source and freely available for download. You will need to install this first for the package to work. This is a useful application and some of the options available using this tool include the ability to password protect your zip files.

Add Variables:-

Firstly in SSIS you need to create the variables to be used throughout.

Remember in SSIS variables are case sensitive.

InFileType should contain the extension of the file type, in the example I am zipping txt files, it could be any other file type compatible for zipping or * for files of any type.

InFolder is the Folder where the files you want to zip up reside

OutExecutable is to be populated in the script task so leave blank.

OutFileName is to be populated in the script task so leave blank.

OutFolder is where you want to place the final zip file, it can be the same as the InFolder or any other specified location.

OutMessage is to be populated in the script task so leave blank.

OutSubject is to be populated in the script task so leave blank.

Next add a script task:-

You will need to set the ReadOnlyVaraibles to:

User::InFolder,User::OutFolder,User::InFileType

And the ReadWriteVariables to:-

User::OutExecutable,User::OutSubject,User::OutFileName,User::OutMessage

 

Zoom in   |   Open in new window

Then in the Design script add the following scipt:-

Imports System
Imports System.Data.OleDb
Imports System.IO

 

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

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

 

This script creates a zipped file called Archive with a date appended for good measure. Alter the subject (OutSubject) and Message (OutMessage) as required.

Next add a Execute Process task:-

The Executable should point to the location of 7-Zip, and its Working directory as in the diagram.

Zoom in   |   Open in new window

In the Expressions add the OutExecutable as the arguments as in the diagram.

Zoom in   |   Open in new window

Add a SMTP connection

In Connection Managers add a smtp connection pointing to your mail server


Zoom in   |   Open in new window

Lastly add a send mail task:-

Using the Smtp connection that you just created. Add a From and To email address. Normally I set the From connection to be Servername at my companies domain.

Zoom in   |   Open in new window

Next add the global variables in the Expressions:-

You need to add the OutFileName as FileAttachments, the OutMessage as MessageSource and OutSubject as Subject, as shown in the diagram.

Zoom in   |   Open in new window

Link all the task together with On Success connections and you should be ready to go. Your final package should look like this:-

 Hopefully this article has given you a good place to start with zipping files and sending files using SSIS.

By Carolyn Richardson, 2008/09/22

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值