2012-11-02 63 views
-4

有人可以展示如何使用SSIS中的脚本任务枚举给定文件夹中的文件?使用SSIS脚本枚举文件夹中的文件任务

+0

它不是重复的 - 我想通过脚本任务来做到这一点,而不是通过枚举For循环,然后处理文件。 –

+0

脚本任务中有一个很好的枚举用例。我们使用这种方法首先枚举文件,然后根据来自数据库的已处理文件列表检查此列表。这样我们就不需要为每个文件启动一个单独的SQL任务。节省的时间使得这是一个非常有效的请求。 – milivojeviCH

回答

1

配置两个变量:只读字符串User::download_path和读写对象User::files_to_process接收文件列表。

public void Main() 
{ 
    bool fireAgain = true; 

    var filesToProcess = new System.Collections.ArrayList(); 
    var filesInDirectory = new System.Collections.ArrayList(); 

    var download_path = (String)Dts.Variables["User::download_path"].Value; 

    // Find for example all csv files in the directory which are having size > 0 
    var downloadedFiles = new DirectoryInfo(download_path).EnumerateFiles("*.csv",SearchOption.TopDirectoryOnly); 
    foreach (var f in downloadedFiles) 
    { 
     if (f.Length > 0) 
      filesInDirectory.Add(f.FullName); 
    } 

    Dts.Events.FireInformation(3, "Getting files in directory", downloadedFiles.Count().ToString() + " found.", "", 0, ref fireAgain); 

    // Report the file names into the SSIS Log: 
    foreach (var f in filesToProcess) 
    { 
     Dts.Events.FireInformation(3, f.ToString(), "Ready for processing", "", 0, ref fireAgain); 
    } 

    // Return them into the READWRITE object variable 
    Dts.Variables["User::files_to_process"].Value = filesInDirectory; 

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

设置如何查看? – Si8

+0

哪个设置?在SSIS中,这只是一个脚本任务。 – milivojeviCH

+0

我试图添加脚本任务,但它保持失败,所以我直接去了连接管理器的表达式,并添加了一个现在工作正常的表达式。谢谢。 – Si8

相关问题