【问题标题】:Copying all files in SSIS without Foreach在没有 Foreach 的情况下复制 SSIS 中的所有文件
【发布时间】:2014-04-18 17:19:27
【问题描述】:

是否可以不使用 foreach 将所有文件从一个文件夹复制到另一个文件夹?

我的来源是 c:\test1*.txt

目的地为 c:\test2

当我使用文件系统任务执行此操作时,我收到以下错误

 An error occurred with the following error message: "Illegal characters in path.".

【问题讨论】:

    标签: ssis


    【解决方案1】:

    是的,可以将所有文件从一个文件夹复制到另一个文件夹。下面,我的源是 C:\test1,我的目标是 C:\test2。下面的任务会将所有文件从 C:\test1 复制到 C:\test2。

    您遇到的错误是由于源代码中的星号造成的。您是否尝试使用通配符?文件系统任务不允许使用通配符。查看File System Task 上的文档,以下是摘录:

    文件系统任务对单个文件或目录进行操作。 因此,此任务不支持使用通配符 对多个文件执行相同的操作。拥有文件 系统任务对多个文件或目录重复一个操作,把 Foreach 循环容器中的文件系统任务,如 以下步骤:

    • 配置 Foreach 循环容器 Foreach 循环编辑器,将枚举器设置为 Foreach File Enumerator 和 输入通配符表达式作为枚举数配置 文件。在 Foreach 循环编辑器的变量映射页面上,映射一个 您想用来一次一个地将文件名传递给的变量 文件系统任务。

    • 添加和配置文件系统任务 文件系统任务到 Foreach 循环容器。在常规页面上 文件系统任务编辑器,设置 SourceVariable 或 DestinationVariable 属性添加到您在 Foreach 循环容器。

    另一种选择是在脚本任务中编写复制例程:

    string fileName = string.Empty;
    string destFile = string.Empty;
    string sourcePath = @"C:\test1";
    string targetPath = @"C:\test2";   
    
    // Create a new target folder, if necessary. 
    if (!System.IO.Directory.Exists(targetPath))
    {
        System.IO.Directory.CreateDirectory(targetPath);
    }
    
    if (System.IO.Directory.Exists(sourcePath))
    {
        string wildcard = "*.txt";
        string[] files = System.IO.Directory.GetFiles(sourcePath, wildcard);
    
        // Copy the files and overwrite destination files if they already exist. 
        foreach (string s in files)
        {
            fileName = System.IO.Path.GetFileName(s);
            destFile = System.IO.Path.Combine(targetPath, fileName);
            System.IO.File.Copy(s, destFile, true);
        }
    }
    else
    {
        throw new Exception("Source path does not exist!");
    }
    

    【讨论】:

    • 感谢您抽出时间回答这个问题。可以这么说,不使用 Foreach 是不可能进行这种操作的。
    • 你误会了......它可能(并且更容易)在执行过程任务中做到这一点。
    【解决方案2】:

    或者一个包含这个的执行流程任务:

    COPY c:\test1*.txt c:\test2
    

    此代码在减少键盘磨损方面的效率是脚本任务的 25 倍。 :p

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2021-09-18
      • 2021-01-10
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多