Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
You mean a recurssive delete? This can indeed be done. Something like rd /s /q "c:\folder a will perform a recursive delete on all files and folders within Folder A –  Ramhound Apr 15 '14 at 12:47
    
I could do, I dont know how to script in powershell but have previous run powershell scripts. –  BobJim Apr 15 '14 at 13:41
2  
Now is a perfect time to learn, I would research batch and vbs syntax for legacy purposes, but invest more time in learning PS. –  BigHomie Apr 15 '14 at 14:26
    
I havent had the chance to check the code yet although I'm sure it will work. I will come back and select the most appropiate answer to my initial query. –  BobJim Apr 17 '14 at 7:08
    
I'll second @BigHomie's recommendation - I just learned some PowerShell and I'd have to say it's pretty neat - not too hard, a good weapon in any coder's arsenal. I use it fairly frequently now. –  Steve Dec 19 '14 at 17:13

7 Answers 7

up vote 16 down vote accepted

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.

share|improve this answer
    
Have run the PS script for removing all files including those without extension but I'm getting this error:Get-ChildItem : A parameter cannot be found that matches parameter name 'File'. At line:1 char:56 + Get-ChildItem -Path D:\EmptyThisFolder -Include . -File <<<< -Recurse | foreach { $_.Delete()} + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand –  BobJim Apr 25 '14 at 8:09
    
It's -Include *.* not -Include ., big difference! :-) –  BigHomie Apr 25 '14 at 11:00
    
Even when I run the following code I still get this error: –  BobJim Apr 25 '14 at 13:06
    
Get-ChildItem -Path D:\EmptyThisFolder -Include . -File -Recurse | foreach { $_.Delete()} –  BobJim Apr 25 '14 at 13:07
    
Why are you using -Include . ?? That's what the problem is. –  BigHomie Apr 25 '14 at 13:08

You can do so with del command:

dir C:\folder
del /S *

The /S switch is to delete only files recursively.

share|improve this answer
1  
Will this only delete files but leave the folders? –  BobJim Apr 15 '14 at 12:56
4  
Yes. You can add /P option so it will ask you to confirm every delete just to check that. –  edvinas.me Apr 15 '14 at 12:59
    
thanks ill give it a go! hopefully will save loads of time! –  BobJim Apr 15 '14 at 13:11
    
FYI - this will only delete files you have access to. This will not delete hidden or system files. –  Keltari Apr 15 '14 at 13:33
2  
Yes. And for deleting hidden and system files, do attrib -s -r -h Folder\*.* /s /d before deleting. And for permissions use takeown command. –  Jet Apr 25 '14 at 13:49

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"
share|improve this answer
    
Thanks for the idea but currently I think the "emptying" process will be completed on a very unscheduled adhoc time frame. I'll keep that code for the future though! Cheers –  BobJim Apr 15 '14 at 13:42

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}
share|improve this answer
  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.

share|improve this answer

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 
share|improve this answer

Using PowerShell:

Get-ChildItem -Path c:\temp -Include * | remove-Item -recurse 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.