delete files in a directory older than a month

I am attempting to create a sql command that will go to a given directory and delete out all files older than a month.

How do i do this?
DJMoonLightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ptjcbCommented:
You could do this much easier with a windows script than t-sql. For example: http://www.databasejournal.com/scripts/article.php/3517231
You could schedule it as a job and run it as a CMDEXEC.

To use t-sql you would have to use a filesystem object and DMO. Lots of code...

I found this at this link (I have not tried it - I use VBScript):http://www.thescripts.com/forum/thread78656.html

Create procedure USP_DelOldFiles @path varchar(25),@duration int
as
--Objective: To delete files older than certain period from a folder
--Usage example:
--Exec USP_DelOldFiles 'c:\test',30
-- which deletes files older than todaydate-30
--Created by :MAK
--Created date: Jan 7,2003
--OS: windows 2000
declare @myquery varchar(1000)
declare @query varchar(1000)
declare @name varchar(100)
set @myquery = 'exec master.dbo.xp_cmdshell ''dir '+ ltrim(rtrim(@path)) +
'\*.* /a/od'''
print @query

create table #Filenames (id int identity(1,1) ,name varchar(100))

insert #Filenames(name)
exec (@Myquery)
delete from #Filenames
where substring(name,3,1) <> '/'
or name is null
or substring(name,25,1) ='<'

select name, SUBSTRING(name,7,4) + SUBSTRING(name,4,2) + left(name,2)
from #Filenames

/* Make sure dates are in comparable formats */
Declare mycursor cursor for
select name from #Filenames
where SUBSTRING(name,7,4) + SUBSTRING(name,4,2) + left(name,2) <=
CONVERT(char(8),DATEADD(d,@duration,getdate()),112 )

open mycursor

fetch next from mycursor into @name
while @@fetch_status = 0
begin
set @query = 'exec master.dbo.xp_cmdshell ''del '+ @path + '\'+
ltrim(rtrim(substring(@name,40,59)))+''''
print @query
exec (@query)
fetch next from mycursor into @name
end
close mycursor
deallocate mycursor

drop table #Filenames
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
incapitalCommented:
I have to agree... Using the windows scheduler and then a simple utility like delOld.exe would be much simpler.

http://www.michna.com/software.htm#DelOld
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.