SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script to Delete Reports Older than a Certain Number of Days


Script to Delete Reports Older than a Certain Number of Days

Author
Message
Simbo_mk
Simbo_mk
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 19
Hello Everyone!

I am a novist in T-SQL programming. I have an urgent task, as part of the maintenance plan, described as folllows:

Write a script that will delete files from a specific folder every 15 days and send a report after the action is completed.

I came up with the following script, but it does not seem to do the work:
------------------------------

DECLARE @Date nvarchar(10)
DECLARE @Time nvarchar(12)
DECLARE @DTime nvarchar(23)
DECLARE @ShelfLife int

SET @ShelfLife = 15 -- How long to keep backup files (15 days)
SET @Date= CONVERT(date,GETDATE()-@ShelfLife,109)
SET @Time= CONVERT(time(0),GETDATE(),108)
SET @DTime=@Date + 'T' + @Time

EXECUTE master.dbo.xp_delete_file 1,N'E:\BACKUPS\MaintenancePlanReports',N'txt',@DTime,1
--------------------------------

Any suggestions is highly appreciated.

Thanks in advance.
Andrew Kernodle
Andrew Kernodle
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1302 Visits: 8135
Hm. I could be wrong on this, but I believe xp_delete_file will only work on SQL Server backup and report files; that is, files ending in .trn, .bak, and .rpt. It looks like you're trying to delete .txt files with it, which shouldn't work. You may have to resort to using a PowerShell script to do this instead.

- :-D
Simbo_mk
Simbo_mk
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 19
Thank you for your reply and for your suggestion.
I will try and see what this suggestion results to.

Thanks again.
Simbo_mk
Simbo_mk
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 19
I am sorry, but I could not find enough information on what I am trying to accomplish. Here is what I tried, but without success:

EXEC master..xp_cmdshell 'del C:\file.txt'
Andrew Kernodle
Andrew Kernodle
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1302 Visits: 8135
Ah, you'll probably need to create an Agent job and specify it as a Powershell type. Using xp_cmdshell may work, but it doesn't have the flexibility of Powershell. Here's the script I use to clean up transaction log backups older than 14 days, altered a bit:

$a = Get-ChildItem "<drive>:\<folder>" -recurse
foreach($x in $a)
{
$y = ((Get-Date) - $x.CreationTime).Days
if ($y -gt 14 -and $x.PsISContainer -ne $True)
{$x.Delete()}
}

Replace the <drive> and <folder> parts as needed with your filesystem locations, and adjust <if ($y -gt 14) to your desired number of days.

Keep in mind this will delete ALL files older than the specified number of days in the provided location, regardless of type; if you need file-extension filtering, I may be able to alter it as needed. Also, of course, test this in a disposable directory to make sure it's doing what you need it to.

- :-D
SGT_squeequal
SGT_squeequal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1159 Visits: 1118
What about using a vbs script executed by windows scheduled task? I use this method and write out my results to text file.

*************************************************************

The first is always the hardestw00t
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)

Group: General Forum Members
Points: 205063 Visits: 41952
hisakimatama (11/8/2013)
Using xp_cmdshell may work, but it doesn't have the flexibility of Powershell.
...{snip}...
if you need file-extension filtering, I may be able to alter it as needed.


BWAAA-HAAA!!!! Too funny. While Powershell may be able to do certain things that DOS cannot, DOS (via xp_CmdShell) sure does make it look easy to me (notice... I have not tested this script)... even easier than the Powershell script. This one even has filtering for the extension. ;-)

forfiles -p "C:\SomeFolder" -s -m *.txt -d -14 -c "cmd /c del @path"



Shifting gears a bit, there are two things that caught my eye on the original post.

First the title says "Delete Reports", not .txt files. Second, the requirement is to do this from a Maintenance Plan and the related created job. If both are true, there there's no need for either PowerShell or xp_CmdShell. All of the necessary features are avaliable by clicking and dragging in the MP or a simple selection in the job as is the ability to send a notification email for pass/fail, etc.

Considering the wording of the original question, I'm thinking this is either a question on a test or a question on an interview. So, my question to the OP would be, have you tried doing it from a Maintenance Plan because that's what they actually want to see. There is no code required for this.

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Andrew Kernodle
Andrew Kernodle
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1302 Visits: 8135
Huh, so doing that via command-line syntax is possible! Guess I slipped up on that one :-P. I seem to recall trying to do something similar via cmdshell before, and I didn't have a bit of luck with finding information on it. Clearly, I needed more Google-Fu :-D. Duly noted, Jeff!

- :-D
homebrew01
homebrew01
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11716 Visits: 9222
I was also going to mention FORFILES. I use it for a similar purpose: Deleting trace files older than X days. I schedule it through SQL Scheduled Jobs.

forfiles /p "i:\tracefiles" /s /m *.trc /c "cmd /c del @path" /d -45

I think the syntax varies slightly depending on the version of Windows.



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)

Group: General Forum Members
Points: 205063 Visits: 41952
hisakimatama (11/9/2013)
Huh, so doing that via command-line syntax is possible! Guess I slipped up on that one :-P. I seem to recall trying to do something similar via cmdshell before, and I didn't have a bit of luck with finding information on it. Clearly, I needed more Google-Fu :-D. Duly noted, Jeff!


No problem. I just happen to be a big fan of DOS for doing certain things because the commands are sometimes so very easy to use compared (for me at least) to some of the PowerShell stuff (I gave up "programming" way back in 2002 so things like PowerShell have become a bit strange for me).

As a bit of a sidebar, it's really kind of funny. Everyone said DOS was a "dead language" but the ForFiles command wasn't available until (IIRC) Windows Server 2003. Even XP didn't have it. Adding to that, RoboCopy and a couple of other goodies came out with Windows Vista/Windows 2008 and maybe it's not the "dead language" that everyone thought (I say after crossing heart and, with folded hands, looking pleadingly towards the sky :-)).

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search