如何在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'