Home Forums SQL Server 2008 T-SQL (SS2K8) Delete multiple backup files with a wildcard (*)? RE: Delete multiple backup files with a wildcard (*)?

  • Hi guys, need help on old backup deletion.

    DECLARE FileCursor CURSOR FOR

    SELECT FName FROM myFileList

    WHERE FName LIKE '%_OLD%'

    OPEN FileCursor

    FETCH NEXT FROM FileCursor INTO @filename

    WHILE @@FETCH_STATUS = 0

    BEGIN

    set @DeleteDateTime = convert(nvarchar(20),DateAdd(DAY, -7, GetDate())) -- older than 7 days

    EXECUTE master.dbo.xp_delete_file 0,@Path,N'bak',@DeleteDateTime

    FETCH NEXT FROM FileCursor INTO @filename

    END

    CLOSE FileCursor

    DEALLOCATE FileCursor

    So I tried this script, and yeah, it'll delete the files older than 7 days, BUT, it deletes all .bak files.

    I need to have an exemption for filenames with %_%

    I'll have to put in a separate condition to delete .bak files with %_%, older than 90 days.

    Can someone help me on this? Thanks a lot in advance! 😉