title: VBScript to delete a folder
body: <p>I'm using below code to delete the folder since it is used by other application I'm getting an error:</p>
  
  <blockquote>
    <p>Permission denied-Cannot delete a folder</p>
  </blockquote>
  
  <p>I need a code to check if the folder is in use by other application, if so kill that process and then delete that folder.</p>
  
  <pre><code>Set fso = CreateObject("Scripting.FileSystemObject")
  Set folder = fso.GetFolder("C:\Users\x")
  'delete all files in root folder
  For Each f In folder.Files
    On Error Resume Next
    name = f.Name
    f.Delete True
    If Err Then
      WScript.Echo "Error deleting:" &amp; name &amp; " - " &amp; Err.Description
    Else
      WScript.Echo "Deleted:" &amp; name
    End If
    On Error GoTo 0
  Next
  'delete all subfolders and files
  For Each f In folder.SubFolders
    On Error Resume Next
    name = f.Name
    f.Delete True
    If Err Then
      WScript.Echo "Error deleting:" &amp; name &amp; " - " &amp; Err.Description
    Else
      WScript.Echo "Deleted:" &amp; name
    End If
    On Error GoTo 0
  Next
  </code></pre>