SSIS - check for multiple files existing
I have an SSIS package in VS 2010 that uses flat files to load database tables.  I would like to check for the flat files existing before continuing to run the package.  The flat files each have their own connection manager.  I was wondering if I could use the connection managers to determine the file names instead of creating a Script Task and hard-coding each of the file names to check.
August 20th, 2015 4:42pm

What do you mean by "hard-coding each of the file names"?  The connection string for connection manager can be dynamic and change during execution if you want.  Also, if you use a script task, you can retrieve the connection string using something similar below.

Dts.Connections["MyFlatFileConnectionManager"].ConnectionString

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:53pm

I've never used a Script Task before.  When I go into the Script Task Editor and select Edit Script, it opens up a new instance of VS.  When I uncomment the following code and enter one of my file connection managers, I get errors. I guess by hard-coding, I meant entering the file names as variables. 

         * Example of using a File connection manager

         *  object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);

         *  string filePath = (string)rawConnection;

         *  //Use the connection in some code here, then release the connection

         *  Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);

August 20th, 2015 5:06pm

Yeah, connection managers take a while to get used to in the script task.

object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);

This statement is a bit dangerous since it may throw an exception. It is essentially attempting to "connect" to the file defined by the connection manager "Prices.zip".  The connection is then stored as the object rawConnection.

string filePath = (string)rawConnection;

This statement returns the file path from the "Prices.zip" connection manager.

Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);

The last statement releases the connection to the file specified by the "Prices.zip" connection manager.

If you just want to check if the file exists, use the statement below in conjunction with the File.Exists method.

Dts.Connections["MyFlatFileConnectionManager"].ConnectionString

File.Exists
https://msdn.microsoft.com/en-us/library/system.io.file.exists%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 5:17pm

Yeah, connection managers take a while to get used to in the script task.

object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);

This statement is a bit dangerous since it may throw an exception. It is essentially attempting to "connect" to the file defined by the connection manager "Prices.zip".  The connection is then stored as the object rawConnection.

string filePath = (string)rawConnection;

This statement returns the file path from the "Prices.zip" connection manager.

Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);

The last statement releases the connection to the file specified by the "Prices.zip" connection manager.

If you just want to check if the file exists, use the statement below in conjunction with the File.Exists method.

Dts.Connections["MyFlatFileConnectionManager"].ConnectionString

File.Exists
https://msdn.microsoft.com/en-us/library/system.io.file.exists%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

  • Marked as answer by pdconway 14 hours 59 minutes ago
August 20th, 2015 9:02pm

Thanks for your help.  That was just what I was looking for!
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 12:15pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics