Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Greetings,
I need help with writing a batch file which will delete ALL the folders EXCEPT a few. FOr example, I have few directories named A,B,C,D etc in C:\tempNow I want to delete A,B,C and other subdirectories under C:\temp but ONLY KEEP folder "D". I want a command that will KEEP certain directories I specify and delete every other directories.
Merci beaucoup.

Do you want to delete all the "A, B, and C" folders on the computer, or just the folders in the current folder?

Here's one way. I'm sure someone else could come up with something better. This one deletes all the folders with the specified name in the entire computer. To change it so it only deletes the specified folders in the current folder, remove the
/S
from the line,
DIR /AD /B /S %Folder%>Temp.tmp~~~~~DelDir.bat
@ECHO OFF
CD\
SET /P Folder=Which Folder?
DIR /AD /B /S %Folder%>Temp.tmp
FOR /F "Tokens=*" %%F IN (Temp.tmp) DO RD %%F~~~~~DelDir.bat
:Note:
This file restricts you to deleting all folders with the same name at a time.

Ah, curse me, I can never complete the file in one post. Here's the full file, the way it should be:
~~~~~DelDir.bat
@ECHO OFF
SET /P Folder=Which Folder?
DIR /AD /B %Folder%>Temp.tmp
FOR /F "Tokens=*" %%F IN (Temp.tmp) DO RD %%F
DEL Temp.tmp~~~~~DelDir.bat
Once again, I'm sure that there is a much easier and more efficient way.

CWoodward bro, thanks for your help. I will try your solution and post if it worked for me.
To answer your first question, well, referring to my first post, C:\temp folder doesn't get deleted, only some folders inside \temp gets deleted and some stays intact inside \temp.
Its like selecting a few folders to keep and remove others. What makes it difficult is that all the folder names are not known. I only know which ones to KEEP.
Thanks again. Hopefully your solution will work.

hello again.
let me describe my situation again.
i have users loggin in to my labs. what i want to do is, delete their folders fromc:\documents and settings\[user_folder]
and only keep the administrator folder in there.
so i have:
c:\documents and settings\administrator
c:\documents and settings\userA
c:\documents and settings\userB
and so on ...what this script should do is clean "documents and settings" folder by only keeping "administrator" and delete everything else.

Try thid:
::== keepADMN.bat
@echo off
dir /s/b/ad "c:\documents and settings\" > allusers
find /v /i "c:\documents and settings\administrator" < allusers > others
for /f "tokens=*" %%U in (others) do (
echo deltree /y %%U )
:: DONE
=====================================
If at first you don't succeed, you're about average.M2

::== keepADMN.bat
@echo off
dir /s/b/ad "C:\Documents and Settings" > allusers.txt
find /v /i "C:\Documents and Settings\Administrator" < allusers.txt > others.txt
find /v /i "C:\Documents and Settings\All Users" < others.txt > clear.txt
for /f "tokens=*" %%U in (clear.txt) do (
echo rmdir /q %%U )
:: DONE
pause
Hey mechanix, thanks a lot for the code. this is the closest so far. i have modified a little bit for windows xp.
however there is little problem. the path has spaces and dos doesn't see the spaces. for example :rmdir c:\documents and settings\test
will NOT work. the following works:
rmdir "c:\documents and settings\test"
so using your script, when it generates the directory list, they contain spaces. i'm wondering if there's any way it could be accomplished or have the path in a way so that DOS recognizes it.
again, thanks a lot.

Doesn't it work if you change this:
for /f "tokens=*" %%U in (clear.txt) do (
echo rmdir /q %%U )to this:
for /f "tokens=*" %%U in (clear.txt) do (
echo rmdir /q "%%U" )?
If at first you don't succeed, you're about average.M2

![]() |
Crystal Reports and Delph...
|
FOR problems
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |