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
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 19
I would like to thank each one of you for your replies and suggestions.

No, this is not a test question. We have jobs that are supposed to delete files older than a certain number of days. For some reasons, the job is not working on our text files.

Thank you,
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86113 Visits: 41096
hisakimatama (11/8/2013)
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.


Simbo_mk (11/14/2013)
I would like to thank each one of you for your replies and suggestions.

No, this is not a test question. We have jobs that are supposed to delete files older than a certain number of days. For some reasons, the job is not working on our text files.

Thank you,


Like hisakimatama said in the second post on this thread (quoted above, as well), xp_delete_file won't delete .txt files. It works only for backup and report files and they have to have the correct embedded header, to boot.

As a bit of a sidebar, this demonstrates that great care and very careful research/testing should be done before using any undocumented stored procedure.

--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
Simbo_mk
Simbo_mk
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 19
Hell again,

1.) I typed the following Powershell script in my job step:

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

2.) I executed the job

I am getting the following error as a result:

The corresponding line is '{$x.Delete()}'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Exception calling "Delete" with "0" argument(s): "The process cannot access the file 'LogBackup-HourlyRun.txt' because it is being used by another process." '. Process Exit Code -1. The step failed.[/font]


Any suggestions???

Thank you!

Simbo_mk
Andrew Kernodle
Andrew Kernodle
Right there with Babe
Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)

Group: General Forum Members
Points: 796 Visits: 8135
Hm. From the error message, it seems someone or something is using the file you're trying to delete. If you accidentally opened it before you deleted it and kept it open (and I know I've done that myself far, far too many times! :-D), close the file and run the script again. Otherwise, see if someone else has access to the file and opened it for some reason.

- :-D
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86113 Visits: 41096
Simbo_mk (11/14/2013)
Any suggestions???


Yes... use the DOS command I posted instead. :-P

--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
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32361 Visits: 18555
Jeff Moden (11/15/2013)
Simbo_mk (11/14/2013)
Any suggestions???


Yes... use the DOS command I posted instead. :-P


Or do as was suggested earlier and just use the drag and drop feature in the Maintenance Plan. :-D



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

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

Group: General Forum Members
Points: 86113 Visits: 41096
SQLRNNR (12/26/2013)
Jeff Moden (11/15/2013)
Simbo_mk (11/14/2013)
Any suggestions???


Yes... use the DOS command I posted instead. :-P


Or do as was suggested earlier and just use the drag and drop feature in the Maintenance Plan. :-D


If it's the proper file type with the correct header, I agree. If not, then drag'n'drop isn't foing to help here. :-)

--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