Copy excel extension files from various subdirectories with ssis

Asked

Viewed 462 times

2

I To head banging on finding the solution to copy excel extension files from multiple subdirectories with ssis.

I tried to get the foreach loop container to do it but I couldn’t, then they passed me a C# code 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)));
            }
        }
  • 1

    And what’s the problem you’re having?

  • See if it helps you in any way: http://answall.com/q/30797/101 and http://answall.com/a/75171/101

  • The Directory.GetFiles has a third parameter which is SearchOption, passes the value of SearchOption.AllDirectories. By default it will return only the files from the specified directory.

2 answers

0

You don’t need a script, you can just use the properties of the Container Foreach Loop, which lets you search all the files in a directory or sub-directory and extension you want. Maps the result (the filenames) of this Loop to a SSIS variable and inside the container uses the Task "File System Task" which allows you to copy/move/delete/etc the files in each loop. If you don’t understand what I have said, I can explain it better if you like. My Inkedin is the same as my name.

0

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

Simply change your own foreach

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

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

A solution would be first you get all the directories using the GetDirectories and then go through all the directories searching for the files using the 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 method Main 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 extension of excel is xlsx and not xslx.

  • Hello, Pedro, the problem I put to run and not any mistake but does not do anything

  • public void Main()and string filter = "*. xslx"; string directorDestino = @"C: Users sure Onedrive Certainty Consulting CLIENTS -CERTAINTY PRESERVE Cargo";List<string> backgrounds = new List<string>(); backgrounds.Add(@"C: Users sure Onedrive Certainty Consulting CLIENTS -SURE PRESERVE INVOICES-DLOG"); // origins.Add(@"F: Arquivosdepto3"); foreach (var origin in origins) { foreach (string file in Directory.Getfiles(source, filter, Searchoption.Alldirectories)) ː File.Copy(file, Path.Combine(directorDestino, Path.Getfilename(file))); } }

  • Main method missing Static public static void Main(){

  • ta exibindo essa mensagem : &#xA; at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.Scripttask.VSTATaskScriptingEngine.Executescript()

  • So this example I went around in a console project by visual studio.

  • Pablo, I’ve put the txt code in google drive https://drive.google.com/open?id=0B0ApY4QPh5KncWhRS3lWX19JNE I’m using the Integration service where it has a script task name (https://drive.google.com/open?id=0B0ApY4QPh5KnWGxNWUI2QWZYR2c)

  • I’m not sure how ssis works, I’ll owe you this.

  • Even so, thank you so much for taking the time to help. Thank you :D

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.