Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Script to Delete Reports Older than a Certain Number of Days Expand / Collapse
Author
Message
Posted Friday, November 8, 2013 1:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, January 6, 2014 9:10 AM
Points: 10, 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.
Post #1512805
Posted Friday, November 8, 2013 1:42 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 3:11 PM
Points: 566, Visits: 6,213
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.

----------------------------------
My journal of things I'm learning about SQL
Post #1512807
Posted Friday, November 8, 2013 1:53 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, January 6, 2014 9:10 AM
Points: 10, Visits: 19
Thank you for your reply and for your suggestion.
I will try and see what this suggestion results to.

Thanks again.
Post #1512810
Posted Friday, November 8, 2013 2:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, January 6, 2014 9:10 AM
Points: 10, 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'
Post #1512811
Posted Friday, November 8, 2013 3:07 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 3:11 PM
Points: 566, Visits: 6,213
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.


----------------------------------
My journal of things I'm learning about SQL
Post #1512833
Posted Friday, November 8, 2013 4:41 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 14, 2014 3:27 PM
Points: 270, Visits: 1,011
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 hardest
Post #1512846
Posted Friday, November 8, 2013 4:47 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:54 PM
Points: 36,722, Visits: 31,171
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1512850
Posted Saturday, November 9, 2013 6:27 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 3:11 PM
Points: 566, Visits: 6,213
Huh, so doing that via command-line syntax is possible! Guess I slipped up on that one . 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 . Duly noted, Jeff!

----------------------------------
My journal of things I'm learning about SQL
Post #1512903
Posted Saturday, November 9, 2013 8:53 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 9:19 AM
Points: 2,826, Visits: 8,462
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.



Post #1512914
Posted Saturday, November 9, 2013 12:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:54 PM
Points: 36,722, Visits: 31,171
hisakimatama (11/9/2013)
Huh, so doing that via command-line syntax is possible! Guess I slipped up on that one . 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 . 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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1512923
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse