Copy excel extension files from multiple subdirectories with ssis

0

I'm banging my head at finding the solution to copy excel extension files from multiple subdirectories with ssis.

I tried to make the foreach loop container do it but could not, then passed me a code in C # to insert into a ScripTask

string filtro = "*.xslx";
        string diretorioDestino = @"C:\Arquivos\Destino";

        List<string> origens = new List<string>();
        origens.Add(@"D:\ArquivosDepto1\");
        origens.Add(@"F:\ArquivosDepto3\");


        foreach (var origem in origens)
        {
            foreach (string arquivo in Directory.GetFiles(origem, filtro))
            {
                File.Copy(arquivo, Path.Combine(diretorioDestino, Path.GetFileName(arquivo)));
            }
        }
    
asked by anonymous 21.02.2017 / 20:50

2 answers

0

Ednilton, Directory.GetFiles returns the names of files (including paths) in the specified directory. For you to list all files, including those for subdirectories, you need to pass more than SearchOption .

Just by simply changing your foreach

foreach (string arquivo in Directory.GetFiles(origem, filtro, SearchOption.AllDirectories))

Eventually Directory.GetFiles may not be allowed to access a particular directory, thus launching a UnauthorizedAccessException .

One solution would be to first get all directories using GetDirectories and then go through all the directories by searching for the files using GetFiles :

public static List<string> GetFiles(string path, string searchPattern, SearchOption searchOption)
{
    var diretorios = Directory.GetDirectories(path);

    List<string> arquivos = new List<string>();
    try
    {

        foreach (var diretorio in diretorios)
        {
            arquivos.AddRange(Directory.GetFiles(diretorio, searchPattern, searchOption).ToList());
        }
    }
    catch (UnauthorizedAccessException)
    {

    }

    return arquivos;
}

The Main method would look like this:

public static void Main()
{
    string filtro = "*.xlsx";
    string diretorioDestino = @"D:\temp";
    List<string> origens = new List<string>();
    origens.Add(@"C:\Users\pablotondolo\Documents");
    // 
    foreach (var origem in origens)
    {
        foreach (string arquivo in GetFiles(origem, filtro, SearchOption.AllDirectories))
        {
            File.Copy(arquivo, Path.Combine(diretorioDestino, Path.GetFileName(arquivo)));
        }
    }
}

NOTE: The excel extension is xlsx and not xslx .

    
21.02.2017 / 21:06
0

You do not need a script, you can only use the properties of the Container ForEach Loop, which lets you search all files in a directory or sub-directory and extension you want. It maps the result (the filenames) of that Loop to an SSIS variable and inside the container uses the Task "File System Task" that allows copying / moving / deleting / etc files in each loop. If you do not understand what I said, I can explain it better if you want. My linkedin is the same as my name.

    
16.10.2018 / 18:16