SSIS - 如何将保存在不同文件夹中的 csv 文件复制到单个文件夹

SSIS - How to copy csv file saved in different folders to a single folder

我有一个包,可以将多个 (14) 个带有区域名称的 csv 文件保存到其各自的区域 (14) 文件夹中。现在我想将所有新创建的 14 个 csv 文件复制到一个文件夹中。我怎样才能通过 SSIS 实现它,谁能帮助我实现它。

非常感谢任何帮助。

脚本组件内部:

DirectoryInfo ParentInfo = new DirectoryInfo(ParentDirectoryPath);
FileInfo[] FileArray = ParentInfo.GetFiles("*",SearchOption.AllDirectories);
foreach (FileInfo Fil in FileArray)
{
Fil.CopyTo(Destination Path);
}

未测试,但类似这样

您可以通过使用 C# 获得更具体的方式,但是您可以使用 foreach 文件枚举器。

将路径映射到父文件夹并select遍历子文件夹。

添加 *.csv 作为搜索字符串。

将完整路径映射到变量。

在 foreach 中添加一个文件系统任务来复制文件。

在 SSIS 中有 3 种在目录之间复制文件的方法:

  1. 使用执行进程任务

您可以使用类似的命令来复制文件:

COPY c:\Source\*.txt c:\Destination
  1. 使用脚本任务

您可以编写一个小脚本来遍历目录中的文件并将它们复制到目的地

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!");
}

参考:Copying all files in SSIS without Foreach

  1. 使用文件系统任务

您可以参考以下链接之一,了解 File System Task 的工作原理