2013-03-08 362 views
1

匹配文件名和文件夹名我有四个文件xxxxxxCd999,xxxxCf999,xxxxC999,xxxxD999 ......我需要将这些文件移动到基​​于文件名的相应文件夹,例如文件xxxxxCd999应该被移动到文件夹Cd999,文件xxxxCf999应该被移动到文件夹Cf999,文件xxxC999应该NE移至等文件夹C999 ... 如何在SSIS实现这一目标?如何将文件移动到不同的文件夹,基于在SSIS

我用for each循环容器,分配给源路径,的DestinationPath和文件系统任务使用这些变量的一些变数,但我现在不会Lost N不知道如何着手, 请帮我

+0

已经创建的上述文件夹? – praveen 2013-03-08 06:58:41

+0

是目标文件夹已存在 – LOL 2013-03-08 07:04:10

回答

3

试试这个: -

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

使用另一个变量的包装设计将

enter image description here

  • 创建3可变

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

在用于DestLoc可变上述表达式中,它改变按您的位置

  • foreach循环配置 enter image description here

enter image description here

更改源文件夹位置按需要

  • 钪RIPT任务 - 增加了2个可变如下 enter image description here

  • 您需要从变量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集合变量。

  • 文件系统任务配置

enter image description here

+0

嗨,我有类似的要求。当我跟踪你的进程时,出现错误提示:“[文件系统任务]错误:出现以下错误消息时出错:”给定的路径格式不受支持“ ” – 2016-04-20 09:21:10

相关问题