none
SSIS C# copy files from folder to newly created folder RRS feed

  • Question

  • Hi,
    I'm currently trying to create new directories and copy files as part of a SSIS package, however the file copy doesn't complete. The folder creation works fine and the whole task completes successfully but the copy does not take place.
    I'm writing this all as a script task because I also have to complete both the above tasks and to replace character strings. I've had issues with the foreach loop container bug deleting code so would like to avoid using these control flow items.

    The code could probably do with refining as well as I'm repeating variables, so I wondered if anyone had any advice on where I'm going wrong and how to streamline?

    public void Main()
    {

    {

    var Today = DateTime.Today;
    string pathyear = Today.ToString ("yyyy");
    string pathmonth = Today.ToString ("MMMM");
    string pathdate = Today.ToString ("dd");

    string folder = Path.Combine(@"C:\Archive\", Path.Combine (pathyear, Path.Combine (pathmonth, pathdate)));

    Directory.CreateDirectory(folder);
    }

    var Today2 = DateTime.Today;
    string pathyear2 = Today2.ToString ("yyyy");
    string pathmonth2 = Today2.ToString ("MMMM");
    string pathdate2 = Today2.ToString ("dd");
    string fileName = string.Empty;
    string destFile = string.Empty;
    string sourcePath = @"C:\Files\";
    string targetPath = Path.Combine(@"C:\Archive\", Path.Combine (pathyear, Path.Combine (pathmonth, pathdate)));

    if (System.IO.Directory.Exists(sourcePath))

    {

    string wildcard = "*.txt";
    string[] files = System.IO.Directory.GetFiles(sourcePath, wildcard);

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

    Monday, November 5, 2018 10:55 AM

Answers

  • I think you'll need to step through this code in SSIS to see why it isn't behaving the way you want. My gut instinct is the folder structure isn't lining up with your expectations.

    Let's start by cleaning up your code. I'm ignoring your block statement at the start because it doesn't seem like it is doing anything. The rest of this code compiles in a console app so it should compile in SSIS although that uses an older version of the framework.

    public void Main ( )
    {
        var sourcePath = @"C:\Files\";
        var baseTargetPath = @"C:\Archive\";
    
        if (!Directory.Exists(sourcePath))
        {
            //Nothing to do, should probably set the script task results here...
            //Dts.TaskResult = (int)ScriptResults.Success;
            return;
        };
    
        var targetPath = Path.Combine(baseTargetPath, DateTime.Today.ToString(@"yyyy\\MMMM\\dd"));
    
        //Make sure the target path exists
        if (!Directory.Exists(targetPath))
            Directory.CreateDirectory(targetPath);
    
        //Get all txt files in the source path, not recursively searching here...
        var files = Directory.EnumerateFiles(sourcePath, "*.txt");
        foreach (var file in files)
        {
            var outFile = Path.Combine(targetPath, Path.GetFileName(file));
    
            File.Copy(file, outFile, true);
        };
    
        //Dts.TaskResult = (int)ScriptResults.Success;            
    }

    Note that I commented out the Dts calls because I wasn't using SSIS.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by lemming3k Tuesday, November 6, 2018 11:04 AM
    Monday, November 5, 2018 3:55 PM
    Moderator

All replies

  • I think you'll need to step through this code in SSIS to see why it isn't behaving the way you want. My gut instinct is the folder structure isn't lining up with your expectations.

    Let's start by cleaning up your code. I'm ignoring your block statement at the start because it doesn't seem like it is doing anything. The rest of this code compiles in a console app so it should compile in SSIS although that uses an older version of the framework.

    public void Main ( )
    {
        var sourcePath = @"C:\Files\";
        var baseTargetPath = @"C:\Archive\";
    
        if (!Directory.Exists(sourcePath))
        {
            //Nothing to do, should probably set the script task results here...
            //Dts.TaskResult = (int)ScriptResults.Success;
            return;
        };
    
        var targetPath = Path.Combine(baseTargetPath, DateTime.Today.ToString(@"yyyy\\MMMM\\dd"));
    
        //Make sure the target path exists
        if (!Directory.Exists(targetPath))
            Directory.CreateDirectory(targetPath);
    
        //Get all txt files in the source path, not recursively searching here...
        var files = Directory.EnumerateFiles(sourcePath, "*.txt");
        foreach (var file in files)
        {
            var outFile = Path.Combine(targetPath, Path.GetFileName(file));
    
            File.Copy(file, outFile, true);
        };
    
        //Dts.TaskResult = (int)ScriptResults.Success;            
    }

    Note that I commented out the Dts calls because I wasn't using SSIS.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by lemming3k Tuesday, November 6, 2018 11:04 AM
    Monday, November 5, 2018 3:55 PM
    Moderator
  • I think you'll need to step through this code in SSIS to see why it isn't behaving the way you want. My gut instinct is the folder structure isn't lining up with your expectations.

    Let's start by cleaning up your code. I'm ignoring your block statement at the start because it doesn't seem like it is doing anything. The rest of this code compiles in a console app so it should compile in SSIS although that uses an older version of the framework.

    public void Main ( )
    {
        var sourcePath = @"C:\Files\";
        var baseTargetPath = @"C:\Archive\";
    
        if (!Directory.Exists(sourcePath))
        {
            //Nothing to do, should probably set the script task results here...
            //Dts.TaskResult = (int)ScriptResults.Success;
            return;
        };
    
        var targetPath = Path.Combine(baseTargetPath, DateTime.Today.ToString(@"yyyy\\MMMM\\dd"));
    
        //Make sure the target path exists
        if (!Directory.Exists(targetPath))
            Directory.CreateDirectory(targetPath);
    
        //Get all txt files in the source path, not recursively searching here...
        var files = Directory.EnumerateFiles(sourcePath, "*.txt");
        foreach (var file in files)
        {
            var outFile = Path.Combine(targetPath, Path.GetFileName(file));
    
            File.Copy(file, outFile, true);
        };
    
        //Dts.TaskResult = (int)ScriptResults.Success;            
    }

    Note that I commented out the Dts calls because I wasn't using SSIS.


    Michael Taylor http://www.michaeltaylorp3.net

    Thanks.
    Yes I suspect it's how I'm setting the targetPath for the file copy, especially with the variable files, so it just doesn't create any output even though the code does compile fine.
    The initial block is for the creation of the folder structure as I don't want any of this to be a manual process, but it looks like you've condensed it nicely into;
       var targetPath = Path.Combine(baseTargetPath, DateTime.Today.ToString(@"yyyy\\MMMM\\dd"));
    That certainly makes it easier to read! I attempted something similar but couldn't get it to work so it'll be interesting to review that.
    EDIT - System.IO wasn't on the using list by default. Though now I've added it EnumerateFiles shows as 'does not have a definition'. Looks like this is because I'm on an older version. Changing it to GetFiles has worked!

    Thank you for your help. I think having a single targetPath seems to have helped a lot.
    • Edited by lemming3k Tuesday, November 6, 2018 11:05 AM
    Tuesday, November 6, 2018 10:41 AM