如何用SQL语句得到文件的日期

bluestar 2008-07-18 03:42:13
我的目的如下:

建立一个作业,每隔一段时间查询备份目录下的备份文件,日期超过一定时间的就删除。
现在卡在检查文件的时间上。

谢谢
...全文
127 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhiguo2008 2008-07-18
  • 打赏
  • 举报
回复
....
tianhuo_soft 2008-07-18
  • 打赏
  • 举报
回复
/* 在SQL Server中取得操作系统文件的最后修改日期
获取文件最后访问日期
@filepath 文件路径,如: c:\1.txt
@filedate 文件最后访问日期

调用示例:
declare @dt varchar(20)
exec getFileLastAccessDate 'c:\1.txt',@dt output
select @dt
*/
create procedure getFileLastAccessDate
@filepath varchar(4000),
@filedate varchar(20) output
as
declare @obj int,@file int
declare @fileexists varchar(10)
exec sp_oacreate 'Scripting.FileSystemObject',@obj output
exec sp_oamethod @obj,'FileExists',@fileexists output,@filepath
if @fileexists='False'
begin
set @filedate='文件不存在'
return
end
exec sp_oamethod @obj,'GetFile',@file output,@filepath
exec sp_oagetproperty @file,'DateLastAccessed',@filedate output
go
/*
获取文件最后修改日期
@filepath 文件路径,如: c:\1.txt
@filedate 文件最后修改日期

调用示例:
declare @dt varchar(20)
exec getFileLastModified 'c:\1.txt',@dt output
select @dt
*/
create procedure getFileLastModified
@filepath varchar(4000),
@filedate varchar(20) output
as
declare @obj int,@file int
declare @fileexists varchar(10)
exec sp_oacreate 'Scripting.FileSystemObject',@obj output
exec sp_oamethod @obj,'FileExists',@fileexists output,@filepath
if @fileexists='False'
begin
set @filedate='文件不存在'
return
end
exec sp_oamethod @obj,'GetFile',@file output,@filepath
exec sp_oagetproperty @file,'DateLastModified',@filedate output
go
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
前面有时间,要是我就用 库名+'2008-01-01'这样的格式做背份名,以后也知道哪天背份的
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
if object_id('tempdb.dbo.#') is not null
drop table #
go
create table #(s varchar(8000))
insert into # exec master..xp_cmdshell 'dir D:\Data'

declare @sql varchar(8000)
select @sql=isnull(@sql+';','')+'exec master..xp_cmdshell ''del D:\Data\'+right(s,charindex(' ',reverse(s))-1)+''''
from # where s like '%.bak'
exec( @sql )


在d盘下键data文件夹,后键几个*.bak测试下
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
if object_id('tempdb.dbo.#') is not null
drop table #
go
create table #(s varchar(8000))
insert into # exec master..xp_cmdshell 'dir D:\Data'
select * from # where s like '%.bak'


2008-07-18 16:09 0 a.bak
2008-07-18 16:09 0 b.bak
2008-07-18 16:09 0 c.bak
bluestar 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wzy_love_sly 的回复:]
用exec master..xp_cmdshell查找文件,截取时间类型的文件名后,再删除
主要
exec master..xp_cmdshell的用法要会
[/Quote]

dir可以返回的是字符串,难道在sql处理字符串来提取时间?
或者有什么其它方法。
bluestar 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangCK 的回复:]
在维护计划里.不是可以设置过期就删除的吗?
[/Quote]

如果是手动备份的呢?文件名都是乱起的,也能删除吗?

-狙击手- 2008-07-18
  • 打赏
  • 举报
回复
。。
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
用exec master..xp_cmdshell查找文件,截取时间类型的文件名后,再删除
主要
exec master..xp_cmdshell的用法要会
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
别非查键文件时间啊,把备份名弄成时间不就行了

liangCK 2008-07-18
  • 打赏
  • 举报
回复
在维护计划里.不是可以设置过期就删除的吗?

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧