如何在SQL SERVER 2000中删除系统文件?
2024-08-31 00:48:19
供稿:网友
/*****************************************************************************************************************
在sql server 2000的开发过程中有时会遇到要判断和删除系统级别的一些文件,这是通过sql server2000提供的一些系统函数可以非常方便的完成相应的功能.下面是我写的一个实列:
author:黄山光明顶
mail:[email protected]
version:1.0.0
date:2004-1-30
(如需转载,请注明出处!)
*********************************************************************************************************/
if exists (select * from sysobjects where id =
object_id('dbo.usp_deletefile') and sysstat & 0xf = 4)
drop procedure usp_deletefile
go
create procedure usp_deletefile
@filename varchar(255),
@path varchar(1024)
as
begin
declare @rc int
declare @space_num int
declare @debug int
declare @temppath varchar(1024)
declare @tempdel varchar(1024)
select @rc=0
select @space_num=0
select @debug=0
select @temppath=''
select @tempdel=''
set nocount on
/***************************************************************************
***********
* check whether @filename is null
****************************************************************************
**********/
if @filename is null
begin
print 'please input file name'
select @rc=-90001
return @rc
end
/***************************************************************************
***********
* check whether @path is null
****************************************************************************
**********/
if @path is null
begin
print 'please input delete file directory(exclude space)'
select @rc=-90002
return @rc
end
/***************************************************************************
***********
* check whether @path includes space
* if the os is windows 2000 ,please confirm the path not includes space!!
****************************************************************************
**********/
select @space_num=charindex(' ',@path)
if @space_num>0
begin
while charindex(' ',@path)>0
begin
select @[email protected]+left(@path,charindex(' ',@path))
select @path=ltrim(right(@path,len(@path)-charindex(' ',@path)))
end
select @[email protected][email protected]
select
@temppath=stuff(replace(@temppath,'/','"/"')+'"',1,3,left(@temppath,2))+'/'+
@filename
if @debug=1
begin
print @temppath
end
end
select @[email protected]+'/'[email protected]
if @debug=1
begin
print @temppath
end
/***************************************************************************
***********
* create temp table to records file information
****************************************************************************
**********/
create table #fileexists(doesexist smallint,fileindir smallint,direxist
smallint)
if @rc=0
begin
insert into #fileexists exec master..xp_fileexist @temppath
if exists (select 1 from #fileexists where #fileexists.doesexist=1)
begin
select @tempdel='del '[email protected]
if @debug=1
begin
print @tempdel
end
exec @rc=master..xp_cmdshell @tempdel,no_output
if @rc<>0
begin
print 'deleting file faile,may be file in useing!'
drop table #fileexists
select @rc=-90003
return @rc
end
else
begin
print 'deleting file sucessfully!'
end
end
else
begin
print 'please check the directory and filename.the input file not
exist!'
end
end
drop table #fileexists
return @rc
end
go
exec usp_deletefile 'test.txt','c:/temp'