delete files

Last Post 25 Jan 2007 12:33 PM by sqladmin. 0 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
sqladmin
New Member
New Member

--
25 Jan 2007 12:33 PM
search terms:
delete files
deleting files
deleting log files
removing files
dropping files
file deletion
file deletes


this is a classic way to delete files, and has been around for quite some time,
but still is a good technique.

it uses the extended procedure: xp_cmdshell

Stored Procedure to delete files in a folder When your only way
of managing SQL Server is through Enterprise Manager or Query
Analyzer, you will have to do a lot of tasks through command
shell. If you find yourself routinely conduct some activities using
xp_cmdshell, it is a good idea to wrap those routines into a stored
procedure. This example takes an input parameter for directory
name and delete all files within that directory. The directory name
can be UNC path. I put some comments within this procedure so it
should be easy to follow.

------------------------------------

CREATE proc usp_DeleteFileInFolder @FolderName varchar(150) as

SET NOCOUNT ON

declare @DOSCommand varchar(150)
--Check whether the user supply \ in the directory name
if not (right(@FolderName, 1) = '\')
set @FolderName = @FolderName + '\'
--Delete all files within this folder. Note: del *.* only deletes files,
--not folders. Q is the quite switch so DOS will not ask for confirmation
set @DOSCommand = 'del /Q ' + '"' + @FolderName + '*.*' + '"'
print @DOSCommand
exec master..xp_cmdshell @DOSCommand
GO

------------------------------------
original post by: Haidong Ji

remember if you are using 2005 you'll need to enable the
xp_cmdshell feature by going to the surface configuration utility.
You are not authorized to post a reply.

Acceptable Use Policy