• rsampson (10/6/2016)


    Using strictly ssis tasks, for each file enumerator, script task to get file info,

    conditionally delete file, done.

    Works great if the directory is not empty, barfs a warning, then fails after 30 messages.

    I'd like to not get the warnings, how to ?

    I realize this thread is old but the post above necro'd it. 🙂

    If you're trying to delete all of a particular file type (or all files) prior to a certain date, why not avoid having to write loops, write your own conditional tests, and etc, etc? Call a command task that uses FORFILES. You can use a single line of code to delete all files with a certain extension in the "current" directory and all sub-directories.

    Here's an example of listing the "Modified Date" and the "FULL PATH" of all files with a mask of "*.txt" the live in the "C:\Temp" directory and all of its sub-directories (/S is for recursion of all sub directories) that have a "Modified Date" date that occurred prior to 01/01/2016 (mm/dd/yyyy - no other format is possible).

    FORFILES /P "C:\Temp" /M *.txt /S /D -01/01/2016 /C "CMD /C ECHO @FDate @Path"

    Change everything to the right of "CMD /C" to the delete command that you want (probably DEL @Path) and you're done.

    For more information, please see the following two links...

    [font="Arial Black"]FORFILES command[/font]

    [font="Arial Black"]DEL command[/font]

    Personally, I avoid SSIS and PowerShell for things like this. I hate writing loops. Let the machine write its own pseudo-cursors for you, just like in T-SQL set based code.

    As a bit of a sidebar, file deletions are mostly permanent and you can make some terrible mistakes. My recommendation would be to have one command to move the files to an "intended delete" area (RoboCopy works well as a separate command not using FORFILES) and only delete from that area after a given period of time. That will give people the opportunity to miss their files and bug you to put them back, which you'll easily be able to do because you will have them.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)