SSIS File System Task - delete directory issue

  • I'm creating a package which deletes folders when they reach a certain age. It works fine as long as the folder exists.

    If I try to delete a folder that doesn't exist, I get an error and the package fails.

    What I want is for the package to continue to run if there's nothing to delete.

    I've changed FailPackageOnFailure and FailParentOnFailure but these don't make a difference.

    Does anyone know how I can do this?

  • Its late on Friday at the end of a hard week...

    Setting the Fail values is not going to help in this instance because the package is crashing rather than failing.

    How are you verifying that the folder exists?

  • I'm not verifying it exists at all. I'm just deleting it, and when it doesn't exists, then I have a problem. I suppose that's my question now then isn't it, how do I verify that the folder exists?

  • aaron.reese (3/24/2014)


    yup. You might need to use a script component or WMI. Here are a couple of links

    http://www.bidn.com/blogs/mikedavis/ssis/157/ssis-wmi-event-watcher-check-file-exist-creation

    http://www.bidn.com/blogs/DevinKnight/ssis/76/does-file-exist-check-in-ssis%5B/quote%5D

    Use a Script Task, not a Script Component.

    Here's a C# snippet that you should be able to adapt:

    string fldr = @"C:\Temp";

    // If directory does not exist, don't even try

    if (Directory.Exists(fldr))

    {

    Directory.Delete(fldr);

    }

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I used your code and adapted it as there was an issue where the directory couldn't be deleted if it contained files (or it could have been to do with the permissions level - I'm not sure).

    This is what I used in the end.

    string folder = Dts.Variables["FolderToDelete"].Value.ToString();

    if (Directory.Exists(folder))

    {

    string[] files = Directory.GetFiles(folder);

    string[] dirs = Directory.GetDirectories(folder);

    //delete files from folder

    foreach (string file in files)

    {

    File.SetAttributes(file, FileAttributes.Normal);

    File.Delete(file);

    }

    //delete folder

    Directory.Delete(folder, false);

    }

    Thanks for your help. I couldn't have managed without it.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply