Showing posts with label File Delete. Show all posts
Showing posts with label File Delete. Show all posts

Wednesday, February 1, 2017

How can I delete only PDF File from a folder using VBS

Option Explicit
Dim FileSystemObject, RootFolder, SubFolder, File
On Error Resume Next
 Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
 RootFolder = "C:\Users\PRITOM\Downloads"
 
 For Each File In FileSystemObject.GetFolder(RootFolder).Files
  If StrComp(Right(File.Path, 4), ".pdf", vbTextCompare) = 0 Then
   Wscript.Echo File.Path + " Deleted"
   FileSystemObject.DeleteFile File.Path, True
  End If
 Next
 
 For Each SubFolder In FileSystemObject.GetFolder(RootFolder).SubFolders
 Do
  If StrComp(SubFolder.Name, "management", vbTextCompare) = 0 Then
   Exit Do
  End If
  For Each File In FileSystemObject.GetFolder(SubFolder.Path).Files
   If StrComp(Right(File.Path, 4), ".pdf", vbTextCompare) = 0 Then
    Wscript.Echo File.Path + " Deleted"
    FileSystemObject.DeleteFile File.Path, True
   End If
  Next
 Loop Until True
 Next
On Error Goto 0