Results 1 to 9 of 9

Thread: [RESOLVED] How to delete multiple file extension

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Resolved [RESOLVED] How to delete multiple file extension

    I want to delete multiple file extensions like txt, vbs how to do that.

    My.Computer.FileSystem.DeleteFile

    don't allow me to delete multiple files
    < advertising link removed by moderator >

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to delete multiple file extension

    Try something like this:

    Code:
            Dim ExtToFind() As String = {"*.txt", "*.vbs"}
            Dim FilesToDelete = My.Computer.FileSystem.GetFiles("C:\somedirectory\", FileIO.SearchOption.SearchAllSubDirectories, ExtToFind)
            For Each FileName As String In FilesToDelete
                My.Computer.FileSystem.DeleteFile(FileName)
            Next

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to delete multiple file extension

    put he extensions in to an array and loop through the array calling directory.getfiles using the current array element as the filter
    Code:
    Dim exts() as string = {"*.txt", "*.vbs"}
    Dim folder as string = "put the directory path where you want to delete files here"
    Try
        For Each ext as string In file exts
            For Each f as string In system.io.directory.getfiles(folder, ext)
                 system.io.file.delete(f)
            next
        next
    Catch ex as exception
        messagebox.show(ex.message)
    end try
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to delete multiple file extension

    Edit: Kleinma beats me
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: How to delete multiple file extension

    Thanks Kleinma that worked

    But is there a way that i can delete multiple file by doing recursive search

    ie I want to scan all folder and subfolder.
    Last edited by riteshtechie; Feb 12th, 2010 at 12:17 PM.
    < advertising link removed by moderator >

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] How to delete multiple file extension

    That is exactly what my code does.

    Notice the param passed to the GetFiles method that uses the value of SearchAllSubDirectories. That means it will search the directory you specify, and all sub directories under it.

    Just note if you do something like specify C:\ as your directory and tell it to scan all subdirectories, it is going to recurse the entire file system and will take a while.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: [RESOLVED] How to delete multiple file extension

    Yeah oops sorry I did a typo and thought somethings wrong with the code thanks for help.

    How Can I add WIndows folder to "C:\Somedirectory"

    This code is not working

    Dim FilesToDelete = My.Computer.FileSystem.GetFiles(Environ("&#37;systemdrive"), FileIO.SearchOption.SearchAllSubDirectories, ExtToFind)
    < advertising link removed by moderator >

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] How to delete multiple file extension

    systemdrive is going to give you the root letter of the drive windows is installed on.

    You don't include &#37; when accessing them through the environ method.

    So it would just be Environ("systemdrive") which in most cases will return "C:"

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: [RESOLVED] How to delete multiple file extension

    Great thanks.

    Thanks a million.
    < advertising link removed by moderator >

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width