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
Printable View
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
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
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
Edit: Kleinma beats me :)
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.
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.
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("%systemdrive"), FileIO.SearchOption.SearchAllSubDirectories, ExtToFind)
systemdrive is going to give you the root letter of the drive windows is installed on.
You don't include % when accessing them through the environ method.
So it would just be Environ("systemdrive") which in most cases will return "C:"
Great thanks.
Thanks a million.