SSIS For 循环停止工作

SSIS For Loop Stopped working

我的 ssis 包中有一个 for 循环容器,其中包含一个脚本和一个 sql 任务。

我有 3 个变量。

source.string = this is folder location 
file.string = i have used wildcard = *.csv
exist.int = defaulted to 0

我将 inniexpression 值设置为 @Exists=1 并将 evalexpression 值设置为 @Exists=1

在脚本中,我将其设置为查看源变量,如果 file.string 变量存在,则将存在变量设置为 1

问题是它只是循环它应该只在没有文件的情况下循环。看不出我是怎么做错的,在我将变量更改为通配符 *.csv

之前它正在工作

我已经使用另一个包含文件名而不是通配符的变量对其进行了测试,它工作正常问题是在寻找文件名后跟扩展名的通配符时。为什么是这样?我可以不通过通配符变量吗?

我的脚本任务是

      public void Main()
        {
            // TODO: Add your code here
            string Filepath = Dts.Variables["User::Source"].Value.ToString() 
+ Dts.Variables["User::file"].Value.ToString();
            if (
                File.Exists(Filepath))
            {
                Dts.Variables["User::Exists"].Value = 1;
            }

            /// MessageBox.Show (Filepath);
            /// MessageBox.Show(Dts.Variables["Exists"].Value.ToString());
            Dts.TaskResult = (int)ScriptResults.Success;
        }

        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}

根据以上评论,我提出了 2 种不同的解决方案。现在适合您的解决方案是否定的。 2

  1. This one can search for a specific file based on multiple files in your path. It need some tweaking but can be used if you wanna check if a specific file exists with wildcard

  2. This one evaluates to true if any wildcard file is found.

C# 代码 1

Using System.IO:

string Filepath = Dts.Variables["User::Source"].Value.ToString();
            string WildCard = Dts.Variables["User::file"].Value.ToString(); // In Text form @"*.txt";
            string fullpath = Filepath + WildCard;

            //With for loop
            string txtFile = null;
            // Gets all files with wildcard
            string[] allfiles = Directory.GetFiles(Filepath, WildCard);
            
            //Loop through all files and set the filename in txtFile. Do whatever you want here
            foreach(string fileName in allfiles)
            {
                //Check if a file contains something, it could be a prefixed name you only want
                if(fileName.Contains("txt"))
                {
                    txtFile = fileName;
                    if(File.Exists(txtFile))
                    {
                        Dts.Variables["User::Exists"].Value = 1;
                    }
                }
            }

C# 代码 2

 Using System.IO;
 Using System.Linq;

 string Filepath = Dts.Variables["User::Source"].Value.ToString();
            string WildCard = Dts.Variables["User::file"].Value.ToString(); //In text form "*.txt";
            string fullpath = Filepath + WildCard;

            //With bool
            bool exists = Directory.EnumerateFiles(Filepath, WildCard).Any();

            if(exists == true)
            {
                Dts.Variables["User::Exists"].Value = 1;
            }

              
            MessageBox.Show (Filepath);
            MessageBox.Show(Dts.Variables["Exists"].Value.ToString());