136

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?

RobN
  • 1,812
  • 5
  • 19
  • 28

16 Answers16

149

Short and sweet PowerShell. Not sure what the lowest version of PS it will work with.

Remove-Item c:\Tmp\* -Recurse -Force
tobassist
  • 128
Evan Nadeau
  • 1,631
128

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

MDMoore313
  • 6,336
23

You can do so with del command:

dir C:\folder
del /S *

The /S switch is to delete only files recursively.

ek9
  • 3,455
5

Using PowerShell:

Get-ChildItem -Path c:\temp -Include * | remove-Item -recurse 
bwDraco
  • 46,683
4

Use PowerShell to Delete a Single File or Folder. Before executing the Delete command in Powershell we need to make sure you are logged in to the server or PC with an account that has full access to the objects you want to delete.

With Example: http://dotnet-helpers.com/powershell-demo/how-to-delete-a-folder-or-file-using-powershell/

Using PowerShell commands to delete a file

Remove-Item -Path "C:\dotnet-helpers\DummyfiletoDelete.txt"

The above command will execute and delete the “DummyfiletoDelete.txt” file which present inside the “C:\dotnet-helpers” location.

Using PowerShell commands to delete all files

Remove-Item -Path "C:\dotnet-helpers*.*"

Using PowerShell commands to delete all files and folders

Remove-Item -Path "C:\dotnet-helpers*.*" -recurse

-recurse drills down and finds lots more files. The –recurse parameter will allow PowerShell to remove any child items without asking for permission. Additionally, the –force parameter can be added to delete hidden or read-only files.

Using -Force command to delete files forcefully

Using PowerShell command to delete all files forcefully

Remove-Item -Path "C:\dotnet-helpers*.*" -Force

4

Reading between the lines on your original question I can offer an alternative BATCH code line you can use. What this will do when ran is only delete files that are over 60 days old. This way you can put this in a scheduled task and when it runs it deletes the excess files that you don't need rather than blowing away the whole directory. You can change 60 to 5 days or even 1 day if you wanted to. This does not delete folders.

forfiles -p "c:\path\to\files" -d -60 -c "cmd /c del /f /q @path"
Travis
  • 1,054
2

This is the easiest way IMO

Open PowerShell, navigate to the directory (cd), THEN

ls -Recurse * | rm

(Poof) everything is gone...

If you want to delete based on a specific extension

ls -Recurse *.docx | rm

ls is listing the directory

-Recurse is a flag telling powershell to go into any sub directories

* says everything

*.doc everything with .doc extension

| feed the output from the left to the right

rm delete

All the other answers appear to make this more confusing than necessary.

Kellen Stuart
  • 618
  • 3
  • 10
  • 20
1

We can delete all the files in the folder and its sub folders via the below command.

Get-ChildItem -Recurse -Path 'D:\Powershell Practice' |Where-Object{$_.GetType() -eq [System.IO.FileInfo]} |Remove-Item

Or

Get-ChildItem -Recurse -Path 'D:\Powershell Practice' -File | Remove-Item
1

Try this using PowerShell. In this example I want to delete all the .class files:

Get-ChildItem '.\FOLDERNAME' -include *.class -recurse | foreach ($_) {remove-item $_.FullName}
Bacara
  • 111
1
  1. In Windows Explorer select the root dir containing all the files and folders.

  2. Search for *

  3. Sort by Type (All the folders will be at the top and all the files listed underneath)

  4. Select all the files and press Delete.

This will delete all the files and preserve the directory structure.

Emel
  • 11
1

Delete all files from current directory and sub-directories but leaving the folders structure.

(/Q) switch is for asking the user if he is ok to delete

Caution : try it without the /Q to make sure you are not deleting anything precious.

del /S * /Q 
hdoghmen
  • 111
0
dir C:\testx\ -Recurse -File | rd -WhatIf
What if: Performing the operation "Remove File" on target "C:\testx\x.txt".
What if: Performing the operation "Remove File" on target "C:\testx\bla\x.txt".
bertieb
  • 7,543
0

With Powershell 5.1:


$extensions_list = Get-ChildItem -Path 'C:\folder_path\' -Recurse

foreach ( $extension in $extensions_list) {

    if ($extension.Attributes -notlike "Directory") {

        Remove-Item $extension.FullName  
    }
}

It's removes all itens that are not Directory.

$extension.FullName = Item Path

$extension.Attributes = Item Type ( Directory or Archive )

0

PowerShell example.

I found some paths don't play nicely, so using -LiteralPath works in all cases.

Help on which can be found on the MS docs for Remove-Item.

# To delete all files within a folder and its subfolders.

$subDir = "Z:\a path to\somewhere"

Remove the -WhatIf and -Verbose here once you're happy with the result.

Get-ChildItem -LiteralPath $subDir -File -Recurse | Remove-Item -Verbose -WhatIf

Ste
  • 1,352
0

If all your files have an extension and none of your folders have a dot, you can do the following:

Remove-Item C:\Test\* -Include *.* -Recurse

Check the official documentation which provides multiples examples.

Nereis
  • 101
-1

As a complement to the above answers, actually there's no need to use the Get-Childitem and pass the result to the pipeline in the above answers, because the -Include keyword is included in the Remove-Item command

One can simply:

Remove-Item -Include "." "C:\Temp" -Recurse