SSIS: Get any flat file source from folder and cac

2019-01-20 13:35发布

问题:

I'm working in SSIS and Visual Studio 2008. When executed, I need to have the SSIS package perform the following tasks:

  • Check a folder for a file
  • If a file exists take the file and use it as the source for the flat file
  • Store the name of the file into a global variable that I can access in other parts of my package

The package will be run by some other script. Thus we need it to check for the file every time the package runs. We are trying to prevent the scenario where we have to monitor the folder and execute the package manually when the file appears.

Any suggestions?

回答1:

The easiest way would be to set up a Foreach Loop container that has all the "work" of your package inside of it (optionally, you can it as a precursor step and use a conditional expression off of it). Assuming you have 2 variables called FileName (which is what you will have the value assigned to) and an InputFolder variable that contains the "where" we should be looking

ForEach Loop Editor

Collection tab:  
Enumerator = Foreach File Enumerators
Expression: Directory = @[User:InputFolder]
FileSpec: "YD.*"

Retrieve file name
* Fully qualified

Variable Mappings tab:  
Variable: User::FileName 
Index:  0

You can also do this via a script task, if you'd like to see that, let me know.

EDIT This script again assumes you have the variables InputFolder and FileName defined. Create a Script Task Component and check InputFolder as a read only variable, FileName as a read/write variable.

using System;
using System.Data;
using System.IO;  // this needs to be added
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

// namespace will vary 
namespace ST_bc177fa7cb7d4faca15531cb700b7f11.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        public void Main()
        {
            string inputFolder;
            string fileName;
            inputFolder = Dts.Variables["InputFolder"].Value.ToString();

            // File, if exists will look like YD.CCYYMMDD.hhmmss.done
            string fileMask = "YD.*.done";

            // this array will catch all the files matching a given pattern
            string[] foundFiles = null;
            foundFiles = System.IO.Directory.GetFiles(inputFolder, fileMask);

            // Since there should be only one file, we will grab the zeroeth
            // element, should it exist
            if (foundFiles.Length > 0)
            {
                fileName = foundFiles[0];

                // write the value to our global SSIS variable
                Dts.Variables["FileName"].Value = fileName;
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}


回答2:

Here is a possible option. You can achieve this using the Foreach Loop container. Please find the example that I have provided below. Hopefully, that gives an idea.

Step-by-step process:

  1. On the SSIS package, create 3 variables are shown in screenshot #1. Scope CheckFile represents the package name. Variable Folder will represent the folder that you would like to check for the file. Filename represents the file name to check for. Variable FilePath will be the global variable that you will need. It will be filled in with the file path value if the file exists, otherwise it will be empty.

  2. On the package's Control Flow tab, place a Foreach Loop container and a Script Task. Script Task is to showcase that the variable retains the value after the Foreach Loop container execution is complete. Refer screenshot #2.

  3. Configure ForEach Loop container as shown in screenshots #3 and #4.

  4. Replace the Main() method within the Script Task with the code given under the Script task code section. This is to demonstrate the value retained by the variable FilePath.

  5. Screenshots #5 shows no files exist in the path c:\temp\ and screenshot #6 shows the corresponding package execution.

  6. Screenshots #7 shows the file TestFile.txt exists in the path c:\temp\ and screenshot #8 shows the corresponding package execution.

  7. If you would like to process the file when it exists, you can place a Data Flow Task within the Foreach Loop container to do that.

Hope that helps.

Script task code:

C# code that can be used only in SSIS 2008 and above..

public void Main()
        {
            Variables varCollection = null;

            Dts.VariableDispenser.LockForRead("User::FilePath");
            Dts.VariableDispenser.GetVariables(ref varCollection);

            if (String.IsNullOrEmpty(varCollection["User::FilePath"].Value.ToString()))
            {
                MessageBox.Show("File doesn't exist.");
            }
            else
            {
                MessageBox.Show("File " + varCollection["User::FilePath"].Value.ToString() + " exists.");
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }

Screenshot #1:

Screenshot #2:

Screenshot #3:

Screenshot #4:

Screenshot #5:

Screenshot #6:

Screenshot #7:

Screenshot #8:



标签: ssis