Showing posts with label SQL DTS ActiveX. Show all posts
Showing posts with label SQL DTS ActiveX. Show all posts

Wednesday, January 20, 2010

Move a file with ActiveX in DTS

Once you process a file, you will usually archive it. Often, you may run into cases where the file already exists in the archive directory. Unfortunately, the Move command does not have an overwrite option. You will need to use the Copy command with the Overwrite option.

Function Main()
Dim oFSO
Dim vSourceFile
Dim vDestinationFile

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const OverwriteExisting = True
Const DeleteReadOnly = True

vSourceFile = "\\MySourceDirectory\*.csv"
vDestinationFile = "\\MyDestinationDirectory\"

' Move the files Use OverwriteExisting in case the file is there
objFSO.CopyFile vSourceFile, vDestinationFile, OverwriteExisting

' Delete the files in the source directory
objFSO.DeleteFile(vSourceFile ) , DeleteReadOnly

Set oFSO = Nothing

Main = DTSTaskExecResult_Success


End Function


Now, all of the files in the source directory are now in the archive.

Friday, December 04, 2009

Loop through files in a folder Use ActiveX

There are times when you need to process all files in a folder. SSIS gives us the While loop that can parse all files in a folder. However DTS requires a work around with ActiveX and the trusty FileSystemObject. Here is some code that parses through all the files in a folder and pops up a message box with the file name.

'**********************************************************************
' Visual Basic ActiveX Script
'**********************************************************************


Function Main()

'Set the target folder name
folder = "\\MyDrive\Myfolder\"

set fso = CreateObject("Scripting.fileSystemObject")
set fold = fso.getFolder(folder)

' Loop through the files in the folder
for each file in fold.files

' Pop up a Message Box with the file name
MsgBox( file.name)

' You can also set global variables with this option
'set DTSGlobalVariables("gv_FileName").Value = file.name

next

set fold = nothing
set fso = nothing

Main = DTSTaskExecResult_Success

End Function

Thats it! This script is also handy for files that don't have a predefined name, such as filenames that contain a timestamp (ie Filename_YYYYMMDDHHMMSS.csv)

Monday, December 29, 2008

Using ActiveX to check the age of a file

Sometimes you need to check the age of a file before proceeding. You really don't want to work with old data, so this is how you can do this in the middle of an ActiveX task.

Option Explicit

Function Main()
Dim oFSO, oConn, sFileName, oFile

' Get the filename from my Text File connection called "Text File (Source)"
' There are other ways to do this, of course.
Set oConn = DTSGlobalVariables.Parent.Connections("Text File (Source)")
sFilename = oConn.DataSource
Set oConn = Nothing

Set oFSO = CreateObject("Scripting.FileSystemObject")

' Check File Exists first
If Not oFSO.FileExists(sFilename) Then
' Return Error
Main = DTSTaskExecResult_Failure
Else
' Get file object
Set oFile = oFSO.GetFile(sFilename)

' Check age of file is less than 24 hours
If DateDiff("h", oFile.DateLastModified, Now) >= 24 Then
' Return Error
Main = DTSTaskExecResult_Failure
Else
' Return Success
Main = DTSTaskExecResult_Success
End If
End If

Set oFile = Nothing
Set oFSO = Nothing

End Function

Monday, February 18, 2008

Using ActiveX to Dynamically set a filename

From time to time, you will need to dynamically set a filename in the middle of your DTS packages. The following code shows you how easy it is to build a filename dynamically:

'*************************
' Sets the file name to the format: MyFile_YYYYMMDD.csv
'*************************
Function Main()

Dim oPkg, oBulkExport

'Set the Path where the file is located
strPath = "c:\MyPath\"

'Next build the different parts of the file name,
'including the prefix and
file extension using
'the Now function, which returns the current date and

'the Month(),Day(), and Year() function which
'return the respective
datepart.
strPrefix = "MyFile_"
strMonth = Month(Now)
strDay = Day(Now)
strYear = Year(Now)
strFileExt = ".csv"

'If you only require 2 digit years, trim the first 2 digits
'strYear = Right(strYear, 2)

'If the day or month is a single digit, the
'Now function will only return that single digit

'We will append a leading zero to the Month
'and/or Date string if needed

If Len(strMonth) = 1 Then strMonth = "0" & strMonth
If Len(strDay) = 1 Then strDay = "0" & strDay

'Concatenate all parts of the file name with
'the file path in front.

outString = strPath & strPrefix & strYear & strMonth & strDay & strFileExt

'Once the path and file name are determined,
'set the global variable.

'You will still need to set the file name
using a Dynamic Properties Task.

DTSGlobalVariables("gv_FileName").Value = outString

Main = DTSTaskExecResult_Success
End Function

As stated in the code, you will still need a Dynamic Properties Task to actually set the file name, as you are only setting a global variable in the code.

Thursday, February 22, 2007

Change a Date Format Using ActiveX and DTS

Lets say you are importing data via DTS and the date is in this format:
YYYYMMDD
So, SQL server isn't going to know what to do with this data. So, you're going to have to use ActiveX copy instead of the plain old Copy Column Task. Here is the code you would use in your ActiveX task within the Data Pump:

'**************************************
' Visual Basic Transformation Script
'**************************************

' Copy each source column to the destination column
Function Main()
DTSDestination("myDate") = left(DTSSource("badDate"),4)+"-"&_
+mid(DTSSource("badDate"),5,2)+"-"+right(DTSSource("badDate"),2)
Main = DTSTransformStat_OK
End Function

Substitute myDate for your destination and badDate for the source. This will give you a nice result in the YYYY-MM-DD format.

You can also expand this technique to include times. You would have to use some two more Mid to pull the date and hour, and the right would pull the minutes.

Happy Coding!