Windows Script – How to Delete All Files from a Folder and Its Subfolders

batch filepowershellscriptvbscriptwindows

I want to remove all files from a folder structure, so I'm left with an empty folder structure.

Can this be achieved in either batch or VBScript scripting?

I have tried a very basic batch command, but this required the user to allow the deletion of each file. This wasn't a suitable solution as there are many hundreds of files and this will increase massively over time.

What can you suggest?

Best Answer

This can be accomplished using PowerShell:

Get-ChildItem -Path C:\Temp -Include *.* -File -Recurse | foreach { $_.Delete()}

This command gets each child item in $path, executes the delete method on each one, and is quite fast. The folder structure is left intact.

If you may have files without an extension, use

Get-ChildItem -Path C:\Temp -Include * -File -Recurse | foreach { $_.Delete()}

instead.

It appears the -File parameter may have been added after PowerShell v2. If that's the case, then

Get-ChildItem -Path C:\Temp -Include *.* -Recurse | foreach { $_.Delete()}

It should do the trick for files that have an extension.

If it does not work, check if you have an up-to-date version of Powershell

Related Question