Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Microsoft SQL Server: Integration Services (SSIS) FAQ

SSIS Tasks -

How to use a File System Task with a Source/Destination Variable by Catadmin
Posted: 13 Oct 06

Oh, the joys of using the File System Task dynamically.  It is a royal pain in the butt. @=)

I'm sure you've seen the wonderful "Error at File Move Task [File System Task]: Variable "myvar" is used as a source or a destination and is empty".  Well, if you haven't found a solution for it yet, this post is for you.

I have a flat file named "MyFile.Year.Month.Day.txt", with the Year, Month and Day portions of it changing constantly, that I want to move from a Holding folder to a Processed folder.  Sounds easy, except when you take into consideration that every day the file name is different.

I used a Script Task, before my actual data transformation, to locate the name of the file.  First, I created three package level variables, TotFileName and TrueFileName and Unprocessed_Loc, as String.  Then, I enter into the Value of Unprocessed_Loc the UNC path the unprocessed files will always be in. I.E. \\MyServer\MyFolder\. Make sure you get that last slash.

Drop and drag the Script Task to the Control Flow window.  Edit it, giving it a friendly name, and on the script tab, put in TrueFileName,Unprocessed_Loc as the ReadOnly Variables.  Enter TotFileName as the ReadWrite Variable.  Click "Design Script".  Here's the script I used to get the variables filled.  


Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()

        Dim TotFileName As String


        TotFileName = CStr(Dts.Variables.Item("Unprocessed_Loc").Value) & LTrim(CStr(Dts.Variables.Item("TrueFileName").Value))

        Dts.Variables.Item("TotFileName").Value = TotFileName

        MsgBox(TotFileName & " is TotFileName")

        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class

Note that the MsgBox stuff is just my personal verification that everything filled up correctly.  You can remove those lines once you're sure the variables have filled correctly. Also, the LTrim is VERY IMPORTANT.  Otherwise, your path will resolve with a space right before your FileName.  

TrueFileName should populate with only the actual FileName.  TotFileName should populate with the entire path.  Yes, maybe it's a little overkill, but I wanted to be sure I covered all my bases. @=)

Once the Script Task is done, close it and create your Flat File Connection Manager.  You'll have to put the literal path in the box titled "File Name" to get your column widths & delimiters set up.  If you don't currently have a file in your "Holding" folder, create a quick fake one with NotePad (including a few sample rows of data) to make sure you get your columns set up correctly.  Then hit "OK" to get out of the Edit Window.

Go to the properties of the Flat File Connection Manager, click into the Expressions box and open the popup window with the ellipses.  Choose ConnectionString under Property and then click the ellipses in the new Expressions column.  Choose "@[User::TotFileName]".  Then click "Ok" again.

The above sets up everything you need to do your data transformation.  Set up the actual pump process however you need to and do your Start Debugging process until you've worked out all the kinks.  It's easier to do it now than to wait until after you've put in your File System Task.

Once all the troubleshooting is done, add your File System Task. When you get into the Edit, make sure IsSourcePathVariable = False and set the SourceConnection to your Flat File Connection.  Unfortunately, setting it to another variable doesn't work unless the variable is a constant, never changing, and the value is filled in on the Variables tab.  Fortunately, the Destination doesn't require the actual FileName, so assuming your "Processed" folder is always going to be in the same place, you just fill in your fully qualified UNC path in the value slot on the Variables tab.

Choose your method (Move File, Copy File, etc.) and click "Ok".  

Debug once more, but it should all come together at this point and smoothly move your dynamically name-changing source file from your Holding folder to your Processed folder when the package runs correctly.

If you have any questions or comments on my process, let me know.  I hope this helps out someone and saves them the hours of headaches I went through figuring this out.  @=)

Back to Microsoft SQL Server: Integration Services (SSIS) FAQ Index
Back to Microsoft SQL Server: Integration Services (SSIS) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close