Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server DBA Tip 10 – SQL Server Reporting – Offload the reporting factorSQL Server Database Administration Tip a Day Series »
    comments

    Deleting OS files from a stored procedure: 

     Here is one way to remove old backup files from the OS through a stored procedure.  The procedure can be compiled in any database.  Once compiled you can run the stored procedure from a scheduled job to remove old backup files from the OS.

     

    IF EXISTS (SELECT [name] FROM dbo.sysobjects WHERE [name] = 'usp_Delete_OS_Files_By_Date' AND TYPE = 'P') 

    DROP PROCEDURE dbo.usp_Delete_OS_Files_By_Date

    GO

    CREATE PROCEDURE dbo.usp_Delete_OS_Files_By_Date (@SourceDir varchar(255), @SourceFile varchar(255), @DaysToKeep int)

    AS

    BEGIN

    /***********************************************************************************    

    Description:  Delete operating system files older than n days                   ****      

    Prototype:

    EXEC dbo.usp_Delete_OS_Files_By_Date                                     

                                          @SourceDir = '\\smpprod01\Backups\SGC\'

                                        , @SourceFile = 'SGC_Backup_*'

                                        , @DaysToKeep = 3                 

                                                                                    ****

    History                                                                         ****

    05/06/2011 Kim Tessereau      Created                                           ****

    ************************************************************************************/

     

    SET NOCOUNT ON;

     

    DECLARE @CurrentFileDate char(10),

    @OldFileDate char(10),

    @SourceDirFOR varchar(255),

    @FileName varchar(255),

    @DelCommand varchar(255);

     

     

    SET @CurrentFileDate = CONVERT(char(10),getdate(),121);

    SET @OldFileDate = CONVERT(char(10),DATEADD(dd,-@DaysToKeep,@CurrentFileDate),121);

    SET @SourceDirFOR = 'FOR %I IN ("' + @SourceDir + @SourceFile + '") DO @ECHO %~nxtI';

     

     

    -- Get OS File information from the OS

     

    CREATE TABLE #Temp_01      ( [lineorder] int IDENTITY(1,1)  

                               , [OSInfo] varchar(255) );

    INSERT INTO #Temp_01

    EXEC master..xp_cmdshell @SourceDirFOR;

     

     

    -- Put the OS File info in date order

     

    CREATE TABLE #Temp_02      ( [lineorder] int   

                               , [TimeStamp] datetime

                               , [FileName] varchar(255) );

    INSERT INTO #Temp_02 ([lineorder], [Timestamp], [FileName])

    SELECT [lineorder]

          , CONVERT (char(10),SUBSTRING([OSInfo],1,10), 121) AS [TimeStamp]                     

          , SUBSTRING([OSInfo],21,255) AS [FileName]                      

    FROM #Temp_01                  

    WHERE [OSInfo] IS NOT NULL               

    ORDER BY [lineorder];

     

     

    -- Loop through OS Information and create delete command and execute

     

    DECLARE DeleteCursor CURSOR READ_ONLY FOR 

    SELECT [FileName]     

    FROM #Temp_02

    WHERE [TimeStamp] <= @OldFileDate;

     

    OPEN DeleteCursor;

     

    FETCH NEXT FROM DeleteCursor INTO @FileName;

     

    WHILE (@@fetch_status <> -1)

     

    BEGIN

          IF (@@fetch_status <> -2)   

                BEGIN     

                SET @DelCommand = 'DEL /Q "' + @SourceDir + @FileName + '"';    

                EXEC master..xp_cmdshell @DelCommand;

                      END 

    FETCH NEXT FROM DeleteCursor INTO @FileName;

    END;

     

    CLOSE DeleteCursor;

    DEALLOCATE DeleteCursor;

     

    END;

    GO

     

    About the Author

    User bio imageI'm a proud mother, dog owner and SQL Architect. I love to teach and am just beginning to get back into bike riding. I work for Swank Motion Pictures and I teach for Washington University CAIT in St. Louis, Missouri
    Social SitingsTwitterFacebookLinkedInWindows Live SpacesLTD RSS Feed
    Instapaper

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)