Copying all files in SSIS without Foreach

孤街浪徒 提交于 2020-01-11 10:22:13

问题


Is it possible to copy all the files from one folder to another without using foreach?

I have source as c:\test1*.txt

and destination as c:\test2

When I execute this using File System Task, I get the following error

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

回答1:


Yes, it's possible to copy all files from one folder to another. Below, my source is C:\test1 and my destination is C:\test2. The task below will copy all files from C:\test1 to C:\test2.

The error you are getting is due to the asterisk in your source. Are you trying to use a wildcard? The File System Task doesn't allow wildcards. Check out the documentation on the File System Task, below is an excerpt:

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

  • Configure the Foreach Loop container On the Collection page of the Foreach Loop Editor, set the enumerator to Foreach File Enumerator and enter the wildcard expression as the enumerator configuration for Files. On the Variable Mappings page of the Foreach Loop Editor, map a variable that you want to use to pass the file names one at a time to the File System task.

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

The other option is to write a copy routine in a Script Task:

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



回答2:


Or an execute process task with this in it:

COPY c:\test1*.txt c:\test2

This code is 25x more efficient at reducing keyboard wear than a script task. :p



来源:https://stackoverflow.com/questions/23158909/copying-all-files-in-ssis-without-foreach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!