The Computer Oracle

Delete all files from a folder and its sub folders

Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4

--

Chapters
00:00 Question
00:40 Accepted answer (Score 117)
01:39 Answer 2 (Score 117)
01:55 Answer 3 (Score 23)
02:12 Answer 4 (Score 5)
02:25 Thank you

--

Full question
https://superuser.com/questions/741945/d...

Question links:
[VBScript]: http://en.wikipedia.org/wiki/VBScript

Accepted answer links:
[PowerShell]: http://en.wikipedia.org/wiki/Windows_Pow...
[check if you have an up-to-date version of Powershell]: https://stackoverflow.com/questions/3820...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #script #powershell #batchfile #vbscript

#avk47



ACCEPTED ANSWER

Score 125


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




ANSWER 2

Score 23


You can do so with del command:

dir C:\folder
del /S *

The /S switch is to delete only files recursively.




ANSWER 3

Score 5


Using PowerShell:

Get-ChildItem -Path c:\temp -Include * | remove-Item -recurse 



ANSWER 4

Score 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"