0

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:

Folder1:

  • File1
  • File2
  • File3

Folder2

  • File1
  • File2
  • File3

Folder3

  • File1
  • File2
  • File3

Etc.

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

EDIT 1

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.

C:\Users\myname\Desktop\project1\english\folder1\file1
C:\Users\myname\Desktop\project1\english\folder1\file2
C:\Users\myname\Desktop\project1\english\folder1\file3
C:\Users\myname\Desktop\project1\english\folder2\file4
C:\Users\myname\Desktop\project1\english\folder2\file5

C:\Users\myname\Desktop\project1\chinese\folder1\file1
C:\Users\myname\Desktop\project1\chinese\folder1\file2
C:\Users\myname\Desktop\project1\chinese\folder1\file2
C:\Users\myname\Desktop\project1\chinese\folder2\file4
C:\Users\myname\Desktop\project1\chinese\folder2\file5

Goal: Delete everything except the following:

C:\Users\myname\Desktop\project1\english\folder1\file1
C:\Users\myname\Desktop\project1\chinese\folder1\file1

5 Answers 5

1

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.

4
  • Praise Crom! That works almost perfectly!
    – xupeikai
    Jan 7, 2013 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, 2013 at 8:09
  • Great solution. Would GCI's -exclude be any use in this scenario?
    – Guy Thomas
    Jan 7, 2013 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, 2013 at 4:48
0

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.

0
@ECHO OFF
FOR /D %%X IN (*.*) DO CALL :loopbody "%%X"
REM Don't "fall through" to :loopbody.
GOTO :EOF

: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
GOTO :EOF

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.

0

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.

http://pastebin.com/QbfatTY8

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

2
  • 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... Jan 7, 2013 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, 2013 at 6:49
0

find Folder1 Folder2 Folder3 -type f ( -name File1 -o -name File3 -o -delete )

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .