how to delete physical files in a folder through sql server 2005 (procedure)

we will send folder path as a parameter, need to delete all the files in the specific folder
NOTE : not the folder

here is my code below. thanks in advance for spending your precious time in it.

EXEC @HR = SP_OACREATE 'SCRIPTING.FILESYSTEMOBJECT',@OLE_FILESYSTEM OUT
EXEC @HR = SP_OAMETHOD @OLE_FILESYSTEM, 'DELETEFILE',NULL, @FILE_PATH
EXEC @HR = SP_OADESTROY @OLE_FILESYSTEM

Line 2 returns -2147352570
thanks
amal
LVL 3
samalrajAsked:
Who is Participating?
 
sameer2010Connect With a Mentor Commented:
Try this:
declare @fpath char(50);
declare @delstring char(100);
set @fpath = 'e:\test';
set @delstring = 'del ' + @fpath + '\*.*';
exec xp_cmdshell @delstring;

Open in new window

0
 
K VDatabase ConsultantCommented:
following command will delte x.txt from c:\
exec xp_cmdshell "del c:\x.txt";
0
 
sameer2010Commented:
Note that you will have to get xp_cmdshell enabled through services configuration.
0
 
samalrajAuthor Commented:
Thank you very much
0
All Courses

From novice to tech pro — start learning today.