I'm using the following code for deleting all the files in a particular folder:
Sub DeleteFilesFromFolder(Folder As String)
If Directory.Exists(Folder) Then
For Each _file As String In Directory.GetFiles(Folder)
File.Delete(_file)
Next
For Each _folder As String In Directory.GetDirectories(Folder)
DeleteFilesFromFolder(_folder)
Next
End If
End Sub
Calling function:
DeleteFilesFromFolder("C:\New Folder")
Now, I want to delete all the *.pdf
documents from new folder. How can I delete only the *.pdf
files from the folder (including the sub-folders)?
To delete matching files: enter *_bad. jpg in the search box, select the results and press Delete or Del.
Deleting multiple files and folders To delete multiple items in a list hold down CTRL (Windows) or Œ˜ (Mac) while clicking on the desired items. Once you have selected all the items you would like to delete, scroll to the top of the file display area and click the Trash button.
To remove a file with a particular extension, use the command 'rm'. This command is very easy to use, and its syntax is something like this. In the appropriate command, 'filename1', 'filename2', etc., refer to the names, plus their full paths.
Directory.GetFiles()
allows you to apply a search pattern and return you the files that match this pattern.
Sub DeleteFilesFromFolder(Folder As String)
If Directory.Exists(Folder) Then
For Each _file As String In Directory.GetFiles(Folder, "*.pdf")
File.Delete(_file)
Next
For Each _folder As String In Directory.GetDirectories(Folder)
DeleteFilesFromFolder(_folder)
Next
End If
End Sub
Check the MSDN link for more information: http://msdn.microsoft.com/en-us/library/wz42302f%28v=vs.110%29.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With