Single &multible files Single &multible files delete

Deleting files from server is a common process that web developers perform every day. Thanks to the .net framework the process is fairly easy when you delete single file. Things go little  complicated when you try to delete selected multiple files from a server folder.

We will use an example here of a scenario that you have a media library web site where the lessons is categorized. . We design the database to contain two tables the first “categories” and the other is “lessons”. The lessons table contains a field called “mp3filelocation” that contains the url of the mp3 of the lesson. The “categories” and the “lessons” is related in a one to many relation. You should set the “delete role” in the relation properties window of the vwd data base diagram to “cascade” . This means when you delete a category all the related lessons will be deleted automatically from the data base but you have to delete the mp3file  from it’s directory from the code behind page.

In our scenario the website admin should have the option of deleting a single lesson also to delete a category with all the related lessons.

Deleting single file:

In the “lesson_delete” page we need to delete the mp3  related to the lesson we are deleting. To accomplish this we will put the code to delete the related file in the “SqlDataSource1_Deleted” event handler. we will simply add a databound label that is bounded to mp3fileurl database field then in the code behind take the file path and delete it.

code example:

Dim LABELfileurl As Label = DirectCast(FormView2.FindControl(“mp3nameLabel”),Label)
Dim filenamemp3 As String = LABELfileurl.Text
If e.AffectedRows >0 Then
If filenamemp3.Length >3
End Try
End If
End If

Code explanation:

1-we used “formview2.findcontrol” function to find the label we want  inside the formview(notice form view is naming container so you cant access the control inside it directly like usual ).

2-we used “directcast” because “findcontrol” function return an undefined control so to define the returning object as a label we use “directcast” function.

3-then we store the mp3file name in a variable .

4-check if the data base row successfully removed using “e.affectedrows>0”

5-then check if there is a mp3file related to the removed lessons.

6-then use the “file.delete” to delete the mp3file this method accept the physical path of the file to delete it we use “server.mappath” to return the physical path .

7-and you finished.

Multiple files deleting:

Thing will be a little complicated here in our scenario we have a group of mp3 files located in a directory called “lessonsmp3” in the category delete page we should remove the files related to this category after removing the selected category row from the database

here is the code example

If e.AffectedRows >0 Then

Dim categoryid As String = categoryddl.SelectedValue.ToString()
Dim file() As FileInfo
Dim i As Integer
Dim searchfor As String

Dim directory As New DirectoryInfo(Server.MapPath(“~/lessonsmp3/”))
searchfor= categoryid &“*”&“.mp3″
file = directory.GetFiles(searchfor)

If file.Length >0 Then
For i = 0 To file.Length –1
End If

End If

code explanation:

1-First we create a variable to carry the id of the category we want to remove “categoryid”.

2-then we instantiate an array of file info type object. This will give us the ability to loop through large number of files and delete or edit each file alone.

3-we define the “i” integer type variable to act as a loop counter.

4-then we define a string variable “searchfor” which will carry the search criteria of the mp3files we want to delete. Notice that we designed the lesson insert page to add the category id in front of the uploaded file name to ease the process of multiple file deleting all we have to do is to search for the category id of the category we need to remove in the start of any file name if we found the id of category we want to remove we delete the file else leave the file inplace.

5-then we define a “directoryinfo” type variable that point to the directory that contains the files that we want to delete from. Notice we used “server.mappath” to get the physical path of the directory from the virtual path which is”~/lessonsmp3/”.

6-then we set the search parameters as we mentioned before. the file should contains the category id in the start of it’s name and mp3 type.

7.then we load the files that present in the selected directory into the “file” array we created before.

8-then check if the array contains files by checking it’s length to be more than zero.

9-then we loop through the array checking if the mp3file have the criteria we set before if the  file belong to the category we want to remove we delete the file also using”file(i).delete()” method.

10-continue looping to the last file in the array and you are finished.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • DotNetKicks
  • Yahoo! Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Technorati
  • Twitter

13,774 comments to Single &multible files delete

  • If I ccomply with this diet for 10 days and if I workout a LOT will I lose
    at least ten pounds?!!

  • I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get setup?
    I’m assuming having a blog like yours would
    cost a pretty penny? I’m not very web smart so I’m not 100% sure.
    Any suggestions or advice would be greatly appreciated.


  • Thanks to my father who stated to me on the topic of this blog,this weblog
    is truly awesome.

  • I quite like reading an article that will make men and women think.
    Also,thanks for allowing for me to comment!

  • My partner and I stumbled over here different web address and thought I may as well check
    things out. I like what I see so i am just following you.
    Look forward to looking into your web page again.

  • There is definately a lot to learn about this topic. I
    love all the points you’ve made.

  • excellent put up,very informative. I ponder why the other experts
    of this sector don’t understand this. You should proceed your writing.
    I’m sure,you have a huge readers’base already!

  • My brother recommended I might like this website. He was
    totally right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!

  • You don’t smooth for all your energies then choose to trade several into primaris powers.

  • You may also benefit from the MZD Connect Infotainment system which ensures that you have the latest connectivity options.

    There are a few things to consider when trying to sell them.

    In watching the metrics of customers,viewers and search numbers we are able to project estimates on where the
    modified car market is going,how its growing and how its swallowing similar markets.

  • Seeking an online instantaneous payday loan is often a quick and simple practice with no infinite questions to solution or steams regarding documentation for you to fax around that other
    kinds of loans generally require You can get rid of every one of the
    tensions regarding your loans and focus on your studies on your own lots of time to imagine for the personal loan. Right now the best suggestion is do without and soon you get paid!

  • Sadly,there is no such thing as a solution to get this unit in-recreation except
    you construct it into the game,find a mod that has it,or bring
    it in with a cheatmod.

  • Pretty nice post. I just stumbled upon your blog and wished to say that I have truly enjoyed surfing around
    your blog posts. After all I’ll be subscribing to your feed and I
    hope you write again soon!

  • Everything is very open with a clear explanation of the challenges.

    It was really informative. Your website is extremely helpful.
    Thank you for sharing!

  • Laundry protected labels that remove the use of messy markers and hold clothes and footwear organized and coming residence.

  • I delight in,lead to I discovered exactly what I was having a look for.
    You have ended my four day long hunt! God Bless you man. Have a nice day.


  • Every beginner is welcome to the Affiliate Advertising
    and marketing 101 sectiuon oof our Lessons.

  • Fantastic website you have here but I was curious
    if youu knew of any discusssion boards that cover the
    same topis talked about iin this article? I’d really love
    to be a part of group where I ccan get feedback from other
    knowledgeable individuals that share the same interest. If
    you hav any suggestions,please lett me know.
    Thamk you!

  • Hi I am so delighted Ifound your site,I really found you by error,
    while I waas researching oon Binng for something else,Anyhow I am here now
    and would just like to say many thanks for a tremendous post
    and a alll round entertaining boog (I also love the theme/design),I don’t have time to
    read it all at tthe minute but Ihave bookmarked it and also
    added in your RSS feeds,so whn I ave ime I wjll be back
    to read a great deal more,Please ddo keep up the fantastic job.

  • This information is invaluable. Where can I find out more?

  • I’ve been browsing online greater than 3 hours lately,but I by no
    means discovered any fascinating article like yours. It’s lovely
    value sufficient for me. In my opinion,if all site owners and bloggers made excellent content as you probably did,
    the net will probably be much more useful than ever before.

  • Dance Pad Typing might help kids learn how to type:or maybe when they could form it
    could help all of them get better and in addition much better!

  • You can unhampered dispose of any sort of necessities like
    credit card expenses,purchasing a institution uniform,educational costs fees,pending dues
    etcetera With regard to rejected job offers,this median starting
    off salary had been $34,853? Steer clear of cash advance establishments that will
    prey on persons looking for most of these loans These loans provide one with extra money when they require it the most.

  • Good site you have here.. It’s difficult to find high quality
    writing like yours these days. I truly appreciate
    people like you! Take care!!

Leave a Reply




You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>