在没有 Foreach 的情况下复制 SSIS 中的所有文件 [英] Copying all files in SSIS without Foreach

查看:27
本文介绍了在没有 Foreach 的情况下复制 SSIS 中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to copy all the files from one folder to another without using foreach?

我的源代码为 c:\test1*.txt

I have source as c:\test1*.txt

目的地为 c:\test2

and destination as c:\test2

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

When I execute this using File System Task, I get the following error

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

推荐答案

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

Yes, it's possible to copy all files from one folder to another. Below, my source is C:\test1 and my destination is C:\test2. The task below will copy all files from C:\test1 to C:\test2.

您遇到的错误是由于源中的星号造成的.您是否尝试使用通配符?文件系统任务不允许使用通配符.查看关于文件系统任务的文档,下面是摘录:

The error you are getting is due to the asterisk in your source. Are you trying to use a wildcard? The File System Task doesn't allow wildcards. Check out the documentation on the File System Task, below is an excerpt:

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

The File System task operates on a single file or directory. Therefore, this task does not support the use of wildcard characters to perform the same operation on multiple files. To have the File System task repeat an operation on multiple files or directories, put the File System task in a Foreach Loop container, as described in the following steps:

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

  • Configure the Foreach Loop container On the Collection page of the Foreach Loop Editor, set the enumerator to Foreach File Enumerator and enter the wildcard expression as the enumerator configuration for Files. On the Variable Mappings page of the Foreach Loop Editor, map a variable that you want to use to pass the file names one at a time to the File System task.

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

Add and configure a File System task Add a File System task to the Foreach Loop container. On the General page of the File System Task Editor, set the SourceVariable or DestinationVariable property to the variable that you defined in the Foreach Loop container.

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

The other option is to write a copy routine in a Script Task:

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 的情况下复制 SSIS 中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆