是否可以在不使用foreach的情况下将所有文件从一个文件夹复制到另一个文件夹?
我的源代码为c:\ test1*.txt
和目的地为c:\ test2
当我使用文件系统任务执行此操作时,我收到以下错误
An error occurred with the following error message: "Illegal characters in path.".
Run Code Online (Sandbox Code Playgroud)
是的,可以将所有文件从一个文件夹复制到另一个文件夹.下面,我的源代码是C:\ test1,我的目标是C:\ test2.下面的任务将所有文件从C:\ test1 复制到C:\ test2.

您获得的错误是由于源中的星号.你想使用通配符吗?文件系统任务不允许使用通配符.查看文件系统任务的文档,下面是摘录:
文件系统任务在单个文件或目录上运行.因此,此任务不支持使用通配符 对多个文件执行相同的操作.要让文件系统任务对多个文件或目录重复操作,请将文件系统任务放在Foreach循环容器中,如以下步骤所述:
配置Foreach循环容器 在Foreach循环编辑器的"收集"页面上,将枚举数设置为Foreach文件枚举器,并输入通配符表达式作为"文件"的枚举器配置.在Foreach循环编辑器的"变量映射"页面上,映射要用于将文件名一次传递到文件系统任务的变量.
添加并配置文件系统任务 将文件系统任务添加到Foreach循环容器.在文件系统任务编辑器的常规页面上,将SourceVariable或DestinationVariable属性设置为您在Foreach循环容器中定义的变量.
另一种选择是在脚本任务中编写一个复制例程:
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!");
}
Run Code Online (Sandbox Code Playgroud)