Server
/************************************************************************************************************** ***
During the development of SQL Server 2000, there are times when you encounter files that you can use to determine and delete system levels, which are some of the system functions provided through SQL Server2000 that are very convenient to perform. Here's a real column I wrote:
Author: Huangshan Bright Top
Mail:leimin@jxfw.com
version:1.0.0
Date:2004-1-30
(If you want to reprint, please specify the source!)
*********************************************************************************************************/
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 spaces) '
Select @rc =-90002
return @rc
End
/***************************************************************************
***********
* Check whether @path includes space
* If the OS is Windows confirm, please do not have the path not includes space!!
****************************************************************************
**********/
Select @space_num =charindex (", @path)
If @space_num >0
Begin
While CHARINDEX (", @path) >0
Begin
Select @tempPath = @tempPath +left (@path, CHARINDEX (", @path))
Select @path =ltrim (Right (@path, Len (@path)-charindex (', @path))
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 ' + @temppath
If @debug =1
BEGIN
Print @tempdel
End
exec @rc =master. xp_cmdshell @tempdel, No_output
If @rc <>0
Begin
print ' Deleting file Faile,may is 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
Exist! '
End
End
drop table #fileexists
return @rc
End
Go
exec usp_deletefile ' test.txt ', ' C:\Temp '
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.