Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Deleting files older then 2 days using xp_cmdshell

Author  Topic 

nitin1353
Constraint Violating Yak Guru

381 Posts

Posted - 2010-01-19 : 12:01:58
Gurus,
I was looking at xp_cmdshell options, the backup file name in my folder is some thing like testdb201001192130.bak i.e <nameofthedb><year><month><date><time>.bak. Now if today is 19th jan and i want to delete files of 18th and 17th and if i try something like select @dt=getdate()-2 ,how will this work with the naming convention of my .bak files?

Nitin

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-01-19 : 12:52:29
Don't look at the filename, instead look at the file properties. See the delete code in my backup stored procedure: http://weblogs.sqlteam.com/tarad/archive/2009/12/29/Backup-SQL-Server-2005-and-2008-DatabasesAgain.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

nitin1353
Constraint Violating Yak Guru

381 Posts

Posted - 2010-01-20 : 10:22:45
Tara,
I did something like this
"declare @dt datetime

select @dt=getdate()-2 --No days to delete
EXECUTE master.dbo.xp_delete_file 0,N'D:\restoredb',N'BAK',@dt"

now if i want to implement the same thing for copy, i meant i want to copy the last two days data only.
Please suggest
Nitin
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-01-20 : 19:40:38
Take a look at my code, just specify xcopy instead of the del command.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -