RSS

Delete files in your Pc by SQL Server

13 Dec

If you have program write his log in files in your PC and you want to delete this file by your program you can make job in sql server to do this operation by ‘Ole Automation Procedures’ in SQL Server

in the first we need to enable ‘Ole Automation Procedures’ usind sp_configure

exec sp_configure ‘Ole Automation Procedures’, 1
go
reconfigure
go

After enabling it we can delete the files with Ole Automation Procedures which using FSO (File System Object) from SQL Server and we need to pass method name ‘DeleteFile’ in the tsql script. You can delete all the files or specific files as well. you can use this code to delete all temp in your drive

DECLARE

@Result int

DECLARE

@FSO_Token int

EXEC

@Result =sp_OACreate‘Scripting.FileSystemObject’, @FSO_Token OUTPUT

EXEC

@Result =sp_OAMethod@FSO_Token,‘DeleteFile’,NULL,‘D:\TestFolder\*.txt’

EXEC

@Result =sp_OADestroy@FSO_Token

 
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s