gpt4 book ai didi

ssis - 在没有 Foreach 的情况下复制 SSIS 中的所有文件

转载 作者:行者123 更新时间:2023-12-01 16:22:15 26 4
gpt4 key购买 nike

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

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

目的地为 c:\test2

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

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

最佳答案

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

enter image description here

您收到的错误是由于源代码中的星号造成的。你想使用通配符吗?文件系统任务不允许使用通配符。查看 File System Task 上的文档,以下是摘录:

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

  • Configure the Foreach Loop container On the Collection page of theForeach Loop Editor, set the enumerator to Foreach File Enumerator andenter the wildcard expression as the enumerator configuration forFiles. On the Variable Mappings page of the Foreach Loop Editor, map avariable that you want to use to pass the file names one at a time tothe File System task.

  • Add and configure a File System task Add aFile System task to the Foreach Loop container. On the General page ofthe File System Task Editor, set the SourceVariable orDestinationVariable property to the variable that you defined in theForeach Loop container.

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

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

关于ssis - 在没有 Foreach 的情况下复制 SSIS 中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23158909/

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