Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Is it possible to delete file via SP

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2010-04-10 : 19:50:46
Is it possible to delete file from windows folder, which is on C drive:

I have a program to download e_maiils, sometime the program is downloaing the same twice and has two duplicate same files in the folder, but this program puts the id as prefix.

Now i can get the list of files which are duplicate via SP, but is there a way i can delete the files from windows folder on C drive?
I have thousands of files like these:
"C:\tempmailsfolder\UID_345_myprjectfile.doc"

Thank you very much for the helpful info.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-10 : 19:52:40
You can delete files using xp_cmdshell and the del DOS command.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2010-04-10 : 19:55:23
Hello Tara,Thanks found the following on net:

http://www.kodyaz.com/articles/delete-file-from-sql-server-xp-cmdshell-ole-automation-procedures.aspx


Microsoft now enables SQL Server administrators to enable or disable the xp_cmdshell extenden procedure and releases the SQL Server products with xp_cmdshell is disabled fby default.
So if you think you are capable of taking the security risks and prevent those risks you can enable xp_cmdshell by using the sp_configure stored procedure.


Enable xp_cmdshell using sp_configure


exec sp_configure
go
exec sp_configure 'xp_cmdshell', 1
-- Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.
go
reconfigure
go



********

xp_cmdshell 'del c:\delete-me-file.txt'
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-10 : 20:00:58
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2010-04-11 : 01:45:02
If you don't want to enable xp_cmdshell - you can use an Agent Job and run using VBScript or operating system commands.
Go to Top of Page
   

- Advertisement -