How do I delete the contents of multiple folders except for specific files?

For example, I have multiple folders which contain the same files, as below:


  • File1
  • File2
  • File3


  • File1
  • File2
  • File3


  • File1
  • File2
  • File3


I want to delete all files from each folder, except for File1 and File3. Can I do this with Powershell?


Thank Crom! You guys are awesome!! We've figured out how to delete the files. However, I forgot to mention that I need to delete other folders also.

For example, here is what my folder structure looks like.



Goal: Delete everything except the following:


You could do something like this in PowerShell:

$toKeep = "File1", "File3"
gci startFolder -Recurse | ? { !$_.PSIsContainer } | ? { $toKeep -notcontains $_.Name } | remove-item -WhatIf

Remove the -WhatIf when you are satisfied it will do what you want it to do.

  • Praise Crom! That works almost perfectly! – xupeikai Jan 7 '13 at 8:00
  • However, I need to also delete other folders and files in the same directory. If possible, could you take a look at my EDIT 1? THANKSNSKNS!!@@!! – xupeikai Jan 7 '13 at 8:09
  • Great solution. Would GCI's -exclude be any use in this scenario? – Guy Thomas Jan 7 '13 at 8:33
  • @Guy, that's a good idea. I've never used -Exclude before myself, but it would certainly be worth a try. – dangph Jan 8 '13 at 4:48

You can do it by making the files you wish to keep hidden, here is some commands i found online:
Via CMD try this:

for /f "delims=" %i in ('dir "My Dir\*.log" /b /s') do attrib +h "%i"
for /f "delims=" %i in ('dir "My Dir" /b /s') do del "%i" /q rmdir /s for /f "delims=" %i in ('dir "My Dir" /b /s /ah') do attrib -h "%i"

First like makes the files you want to keep, hidden (instead of *.log use the names of your files).
Second line delete all the files in 'My Dir'.
And then remove the hidden attribute from the specific files.
I tried it my self and it works.

Good Luck.

FOR /D %%X IN (*.*) DO CALL :loopbody "%%X"
REM Don't "fall through" to :loopbody.

ATTRIB +r +s %1\file1.ext
ATTRIB +r +s %1\file3.ext
DEL %1\*.*
ATTRIB -r -s %1\file1.ext
ATTRIB -r -s %1\file3.ext

In human language:
for each of the sub-directory's (in the folder where you call this batchscript) do the commands in loopbody:
The loopbody sets the files to readonly and system,
then deletes everything (that is not readonly and system),
and finally reset the attributes on the files.


Okay, probably not exactly what you're looking for (a software, GUI, quick and easy option), but this can definitely work: simple programming.

Since the only programming language I know right now that allows for this is PHP, I would set up a virtual Web server with WAMP, then when it's online, I'd browse to "localhost" with my browser, then click my way up to a PHP file I'd have put in C:\wamp\www[\optional folder to keep things clean\file.php].

Aw well, let's go. If you don't find anything more convenient, you can use the code I'll make you which will (probably) work. Probably. Best to test it on something harmless first.

If you get a permission error, because the virtual server is playing outside its playground, you'll have to put the folders you want to delete files in to the www folder. Or you can try to run wamp as administrator or something.


Try this. (Gosh, I sure love to lose my time tonight. Not like I have other urgent work to do... >_> Stupid me.)

  • 2
    Why install all of WAMP when all you need is the PHP interpreter? And holy crap is that ever a lot of code to replace a single command... – Ignacio Vazquez-Abrams Jan 7 '13 at 6:41
  • Mmmmh... Because I didn't know PHP interpreters existed! xD And a lot of code... Well... I don't really know any simpler way of doing this. But I tend to overcomplicate things, so. – Ariane Jan 7 '13 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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