delete files

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

25 Jan 2007 11:33 AM
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


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

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