473,722 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete a file from the Server

I have a table in my database on SQL Server which holds a file name
that refers to a file that is stored on the server. I would like to
create a trigger to delete this file from the server if the row in the
table is deleted. I have been trying to use this command in a trigger
(<filename> is the name and path of the file):

xp_cmdshell "delete <filename>"

If some one could please help I would appreciate it very much. I
would love a code sample if you have one. Thank you so very much.

From,

Ryan
Jul 20 '05 #1
2 8802
On 28 Jul 2003 11:12:32 -0700 in comp.databases. ms-sqlserver,
rv*******@aol.c om (Ryan) wrote:
I have a table in my database on SQL Server which holds a file name
that refers to a file that is stored on the server. I would like to
create a trigger to delete this file from the server if the row in the
table is deleted. I have been trying to use this command in a trigger
(<filename> is the name and path of the file):

xp_cmdshell "delete <filename>"

If some one could please help I would appreciate it very much. I
would love a code sample if you have one. Thank you so very much.


(untried)
xp_cmdshell "cmd /c del <filename>"

--
Ride Free (but you still have to pay for the petrol)

(replace sithlord with trevor for email)
Jul 20 '05 #2
"Ryan" <rv*******@aol. com> wrote in message
news:3d******** *************** ***@posting.goo gle.com...
I have a table in my database on SQL Server which holds a file name
that refers to a file that is stored on the server. I would like to
create a trigger to delete this file from the server if the row in the
table is deleted. I have been trying to use this command in a trigger
(<filename> is the name and path of the file):

xp_cmdshell "delete <filename>"

If some one could please help I would appreciate it very much. I
would love a code sample if you have one. Thank you so very much.

From,

Ryan


You can use xp_cmdshell in a trigger, but this may not be the best approach,
especially if there are very frequent deletes. Calling xp_cmdshell is a
relatively slow operation, and everything will be blocked while the trigger
runs. Also, if there are any problems executing xp_cmdshell, such as a
timeout on a network drive, or a slow file delete, then there may be a
significant impact on your main table. Finally, if someone deletes 20 rows
in a single delete statement, you will have to iterate through them with a
loop or cursor in your trigger, calling xp_cmdshell for each file name, and
that could really cause performance problems.

A more flexible approach might be to use the trigger to put the file name(s)
into a 'pending deletion' table, then have a SQL Server job which checks the
table every few minutes and deletes the file. Using xp_cmdshell for that
would be fine, because you don't care (as much) if it times out. Or even
have an external script read the table and do the deletion from the
operating system side. By doing it that way, any unexpected behaviour won't
impact your main processing.

Simon
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1834
by: Peter A. Schott | last post by:
Got a strange scenario going on here in that I could have sworn this worked yesterday. I am issuing binary retrieval calls to an FTP server, writing to a file, close the file, then removing the file from the remote site. When I do this, I end up with 0 byte files. I was hoping to avoid parsing a list of remote and local files and matching them up that way because it would be easier to remove on successful retrieve. I'm including some...
1
8899
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
1
1902
by: Matt Hamilton | last post by:
I have a simple image gallery where I want to allow users to delete files. The problem I have is that after an image is displayed in the browser, I am not able to delete the file because "The process cannot access the file ... It is being used by another process". I also get this error when trying to delete through explorer on the server. I can delete the file if I stop the Web Server service... Is there a way around this? Here is the...
6
2581
by: I am Sam | last post by:
I keep getting this error and I don't know why: The path is too long after being fully qualified. Make sure path is less than 260 characters. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.PathTooLongException: The path is too long
0
2847
by: smanisankar | last post by:
hi, the following is the full page code for uploading a file to server. since i got no idea to overwrite the file, i want delete the file if the file is already uploaded. i got the folder name and filename of the file to delete from the request.QueryString("path") so i got the above error when i try to delete the file before upload. Please anyone help me to solve out from this error. <%@ Import Namespace="System.IO" %>
1
2360
by: nasirmajor | last post by:
dear all, Please any urgent help regarding following code. i have the following code ================================================================= public void Delete(Object sender, DataGridCommandEventArgs e) { 1 int Sid2 = (int)l.DataKeys;
3
3280
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot (i.e. the root directory). This is the code: <script runat="server"> Sub MoveFile(ByVal obj As Object, ByVal ea As EventArgs) File.Move(fudFileSource.FileName, txtFileDest.Text) 'File.Move("F:\4.jpg", "C:\4.jpg") End Sub
4
25180
by: Sailor1877 via AccessMonster.com | last post by:
I've been reading all the threads about problems with LDB files but my specific problem doesn't seem to be addressed. I have a back end database on a server that I'm unable to run Compact and Repair on because the LDB file is still present. It tells me it is being used by a user "Admin" on my machine (a fact that is echoed when I open the LDB file in Notepad.) Unfortunately there is no username "Admin" on the network. When I try to...
2
15002
by: SCPOS | last post by:
Cannot delete X: It is being used by another person or program. Close any programs that might be using the file and try again. Of course I have tried closing any programs I know of that may be using the file (used X because the filename does not matter...it is a specific log file name to the application). I am logged onto a Windows Server 2003 computer that acts as a server for a LAN, but is physically joined to a domain. The local...
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.