gpt4 book ai didi

ssis - 如何创建一个包,将给定文件夹中的所有文件复制到新文件夹中?

转载 作者:行者123 更新时间:2023-12-04 02:39:08 26 4
gpt4 key购买 nike

我有一个名为“Data”的文件夹,其中包含文件名中具有当前日期的所有文件,例如“myfile 20-08-2011”。现在我想创建一个 SSIS收集 08 月所有文件的包,也就是说,我想按月整理文件并将这些文件复制到一个名为“八月”的新文件夹中。我怎样才能做到这一点?

最佳答案

这是在 Foreach loop container 的帮助下实现此目标的一种可能解决方案, Script TaskFile System Task .您可以在没有文件系统任务的情况下执行此操作。但是,我已经使用它来利用内置的控制流任务来移动文件。该示例是使用 创建的SSIS 2005 .

该示例假定文件将被统一命名。因此,该示例使用格式 文件 DD-MM-YYYY .例如,文件将被命名为 File 29-07-2011 , File 15-08-2011等等。

在 SSIS 包上,创建以下变量。在此示例中,源文件存储在文件夹位置 F:\Temp\ 中。并且文件应该移动到位置 *F:\Temp\Monthwise*。在目标文件夹中,每个月都会有文件夹,例如 7 月、8 月等。

  • 目的地文件夹 变量将保存最终目标文件夹值,如 F:\Temp\Monthwise\August但是这个变量将被分配到脚本任务中的实际值。现在,让我们分配值 F:\Temp\Monthwise\ .这个临时值是为了避免文件系统任务在设计时抛出错误消息。
  • 目的地根将包含实际的根文件夹,应根据月份名称在该根文件夹下创建像七月、八月这样的文件夹。
  • 源文件夹 表示最初存储所有文件的文件夹。在此示例中,源文件夹将为 F:\Temp\
  • 源文件路径 表示实际文件路径。当 Foreach 循环容器循环遍历每个变量时,将为该变量分配单个文件值。为了避免文件系统任务在设计时抛出错误消息,让我们为它分配一些虚拟值 F:\Temp\1.txt .
  • 文件模式 定义应该在给定源文件夹路径中循环的文件模式。让我们分配 *.* ,这意味着所有文件都将被循环。您也可以指定 *.txtFile*.txtMy*.xls等等,这取决于您的要求。
  • MonthStartPosition 表示月份值在文件名中开始的位置。所以,在文件名格式File 29-07-2011 , 07 月从第 9 个字符开始。因此值为 9。
  • 月长 指定要提取的字符数。无论如何,这将是 2 个字符,但我不想硬编码。所以,我创建了一个变量。
  • 月份名称格式 指定应如何创建文件夹。值 MMMM 表示它将创建具有完整月份名称的文件夹,如一月、二月等。如果我们使用值 MMM,则文件夹将创建为 Jan、Feb 等。仅当文件夹不存在时才会创建这些文件夹。

  • Variables

    关于 SSIS 包的 控制流 选项卡,放置一个 Foreach loop container并将其配置为循环遍历变量 SourceFolder 中指定的文件夹使用文件模式变量 FilePattern .当 Foreach 循环容器遍历文件时,文件名将分配给变量 SourceFilePath。我们将使用这个变量来获取脚本任务中的月份值。

    Foreach loop container General

    Foreach loop container Collection

    Foreach loop container Variable Mappings

    在 Foreach 循环容器中,放置一个 Script Task并在脚本任务的脚本部分单击设计脚本...按钮以打开 VSTA 编辑器并粘贴这些屏幕截图后提供的代码。由于示例是在 VS 2005 中创建的,因此代码是在 中编写的。 VB.NET 因为这是 SSIS 2005 中唯一支持的语言.

    Script Task General

    Script Task Script

    Script Task  Code

    脚本任务代码:该代码从变量 SourceFilePath 中获取完整的文件路径值。并仅提取文件名以将其存储在局部变量 FileName 中.

    然后检查是否 MonthStartPositionMonthLength变量分配有适当的非零值。然后提取月份值并将其存储在局部变量 MonthValue 中。 .

    使用 MonthValue ,它使用 DateTime 函数获取完整的月份名称值。值 1 分配给日和年,因为我们只需要月份名称。

    局部变量 FolderName 中的月份名称与 DestinationRoot 值结合以检查文件夹是否存在。如果该文件夹不存在,将创建该文件夹,以便文件系统任务不会失败。

    最后,将完整的目标文件夹值分配给包变量 DestinationFolder .该变量将用于文件系统任务。

    VB.NET code for SSIS 2005
    Imports System
    Imports System.Data
    Imports System.Math
    Imports Microsoft.SqlServer.Dts.Runtime

    Public Class ScriptMain

    Public Sub Main()

    Dim varCollection As Variables = Nothing
    Dts.VariableDispenser.LockForRead("User::SourceFilePath")
    Dts.VariableDispenser.LockForRead("User::DestinationRoot")
    Dts.VariableDispenser.LockForRead("User::MonthStartPosition")
    Dts.VariableDispenser.LockForRead("User::MonthLength")
    Dts.VariableDispenser.LockForRead("User::MonthNameFormat")
    Dts.VariableDispenser.LockForWrite("User::DestinationFolder")
    Dts.VariableDispenser.GetVariables(varCollection)

    Dim SourceFilePath As String = varCollection("User::SourceFilePath").Value.ToString()
    Dim FileName As String = SourceFilePath.Substring(SourceFilePath.LastIndexOf("\") + 1)
    Dim DestinationRoot As String = varCollection("User::DestinationRoot").Value.ToString()
    Dim MonthStartPosition As Integer = Convert.ToInt32(varCollection("User::MonthStartPosition").Value)
    Dim MonthLength As Integer = Convert.ToInt32(varCollection("User::MonthLength").Value)
    Dim MonthValue As Integer = 0
    Dim MonthNameFormat As String = varCollection("User::MonthNameFormat").Value.ToString()
    Dim FolderName As String = String.Empty
    Dim MonthwiseDirectory As String = String.Empty

    If MonthStartPosition > 0 AndAlso MonthLength > 0 Then
    MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength))
    End If

    If FileName.Length > 0 AndAlso MonthValue > 0 Then
    FolderName = New DateTime(1, MonthValue, 1).ToString(MonthNameFormat)
    End If

    MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName)

    If Not System.IO.Directory.Exists(MonthwiseDirectory) Then
    System.IO.Directory.CreateDirectory(MonthwiseDirectory)
    End If

    varCollection("User::DestinationFolder").Value = MonthwiseDirectory

    Dts.TaskResult = Dts.Results.Success
    End Sub

    End Class

    C# code for SSIS 2008 and above
    public void Main()
    {
    Variables varCollection = null;
    Dts.VariableDispenser.LockForRead("User::SourceFilePath");
    Dts.VariableDispenser.LockForRead("User::DestinationRoot");
    Dts.VariableDispenser.LockForRead("User::MonthStartPosition");
    Dts.VariableDispenser.LockForRead("User::MonthLength");
    Dts.VariableDispenser.LockForRead("User::MonthNameFormat");
    Dts.VariableDispenser.LockForWrite("User::DestinationFolder");
    Dts.VariableDispenser.GetVariables(ref varCollection);

    string SourceFilePath = varCollection["User::SourceFilePath"].Value.ToString();
    string FileName = SourceFilePath.Substring(SourceFilePath.LastIndexOf('\\') + 1);
    string DestinationRoot = varCollection["User::DestinationRoot"].Value.ToString();
    int MonthStartPosition = Convert.ToInt32(varCollection["User::MonthStartPosition"].Value);
    int MonthLength = Convert.ToInt32(varCollection["User::MonthLength"].Value);
    int MonthValue = 0;
    string MonthNameFormat = varCollection["User::MonthNameFormat"].Value.ToString();
    string FolderName = string.Empty;
    string MonthwiseDirectory = string.Empty;

    if (MonthStartPosition > 0 && MonthLength > 0)
    {
    MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength));
    }

    if (FileName.Length > 0 && MonthValue > 0)
    {
    FolderName = new DateTime(1, MonthValue, 1).ToString(MonthNameFormat);
    }

    MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName);

    if (!System.IO.Directory.Exists(MonthwiseDirectory))
    {
    System.IO.Directory.CreateDirectory(MonthwiseDirectory);
    }

    varCollection["User::DestinationFolder"].Value = MonthwiseDirectory;

    Dts.TaskResult = (int)ScriptResults.Success;
    }

    在 Foreach 循环容器中,将文件系统任务放在脚本任务之后。如屏幕截图所示配置文件系统任务。

    File System Task

    配置包任务后,“控制流”选项卡应如下所示。

    Control Flow

    让我们测试一下这个包。在此之前,源文件夹 F:\Temp 的内容如下所示。这些文件是虚拟的。因此,大小为 0 KB。

    F Temp Folder

    下面的屏幕截图显示了包的成功执行。

    Success

    下面的屏幕截图显示了如何将文件移动到基​​于月份名称创建的相应目标文件夹。各个文件夹的内容如下所示。

    希望有帮助。

    F Temp

    F Temp Monthwise

    F Temp Monthwise August

    F Temp Monthwise January

    F Temp Monthwise July

    关于ssis - 如何创建一个包,将给定文件夹中的所有文件复制到新文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139172/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com