?
Solved

How do I delete the actual file by using ms-sql?

Posted on 2007-10-05
6
Medium Priority
?
170 Views
Last Modified: 2010-03-19
Hello,
I am just curious, is there anyway that I can delete actual file in the server by using ms-sql?
You know in ASP coding, I can delete, write ,or rename the file by using Server.CreateObject("Scripting.FileSystemObject").

Thank you.
0
Comment
Question by:erin027
6 Comments
 
LVL 4

Expert Comment

by:cjohnsen2
ID: 20026357
Sure.  Try this.

xp_cmdshell "cmd /c del <filename>"
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20026393
run the following query

EXEC master..xp_cmdShell " del <fileName> " 
0
 
LVL 18

Accepted Solution

by:
Yveau earned 1000 total points
ID: 20028472
before you can run a xp_cmdshell, you might need to run this to activate the feature:

sp_configure 'show advanced options', 1
go
reconfigure
go
sp_configure 'xp_cmdshell', 1
go
reconfigure
go

then run the statement that aneeshattingal posted:
EXEC master..xp_cmdShell " del <fileName> "

... and to do it real nice, disable the option again, right after you ran the command by:

sp_configure 'xp_cmdshell', 0
go
reconfigure
go
sp_configure 'show advanced options', 0
go
reconfigure
go

Leaving the xp_cmdshell feature available is a huge security risk !

Hope this helps ...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:erin027
ID: 20038374
Yveau,
Thank you for telling me about the security risk too, but I've tried it and it seems like everything went through well because I do not see any error message, but when I check the file, it's still there.

The Result output came out like this:
The syntax of the command is incorrect.
NULL

Can you tell me what I've done wrong?

This were the coding:
sp_configure 'show advanced options', 1
go
reconfigure
go
sp_configure 'xp_cmdshell', 1
go
reconfigure
go
EXEC master..xp_cmdShell " del <D:\Sites\test\product_images> "
go
sp_configure 'xp_cmdshell', 0
go
reconfigure
go
sp_configure 'show advanced options', 0
go
reconfigure
go


I've checked the directory and it was all correct.
0
 

Author Comment

by:erin027
ID: 20038404
Yveau,
Nevermind. I got it. I shouldn't type the "<" ">" sign.
Thank you.
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20041053
Glad I could be of any help !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question