1

我有四个文件 xxxxxxCd999, xxxxCf999, xxxxC999 , xxxxD999 ...我需要根据文件名将这些文件移动到各自的文件夹中,例如文件 xxxxxCd999 应该移动到文件夹 Cd999 ,文件 xxxxCf999 应该移动到文件夹 Cf999 ,file xxxC999 应该移动到文件夹 C999 等等...我如何在 ssis 中实现这一点?

我已经为每个循环容器使用了一个,为 sourcepath、destinationpath 和文件系统任务分配了一些变量来使用这些变量,但是我现在迷路了,不知道如何继续,请帮助我

4

1 回答 1

3

尝试这个 :-

Foreach Loop枚举源文件夹,路径将存储在变量中。在script task编写代码以使用正则表达式获取文件夹名称。脚本任务值将存储在另一个变量中,该变量将用于File System Task

包装设计将是

在此处输入图像描述

  • 创建 3 个变量

     Name         DataType   Expression
     FolderName    string
     DestLoc       string     "D:\\"+ @[User::FolderName]   
     LoopFiles     string
    

在上面的DestLoc变量表达式中,根据您的位置进行更改

  • ForEach 循环配置 在此处输入图像描述

在此处输入图像描述

根据需要更改源文件夹位置

  • 脚本任务 - 添加 2 变量如下 在此处输入图像描述

  • 您需要从变量中提取文件夹名称LoopFiles

例子

LoopFiles变量将D:\ForLoop\SampleFolder1.txt在运行时

因此,为了从上述变量中​​提取文件夹名称,请使用正则表达式

打开Edit Script并编写以下代码

List<string> filePatterns = null;

public void Main()
 {
     filePatterns = new List<string>();
     filePatterns.Add("Folder1");
     filePatterns.Add("Folder2");
     string fileName = Path.GetFileNameWithoutExtension(Dts.Variables["User::LoopFiles"].Value.ToString());
     Match match = Regex.Match(fileName, string.Join("|", filePatterns.ToArray()));
     Dts.Variables["User::FolderName"].Value = match.Value;
     Dts.TaskResult = (int)ScriptResults.Success;
  }

在上面的代码中,您正在提取文件夹名称并将其存储在变量中FolderName。如果有multiple folders,则只需将 添加folder namesfilePatterns集合变量中。

  • 文件系统任务配置

在此处输入图像描述

于 2013-03-08T07:39:32.270 回答