Computing.Net > Forums > Programming > batch delete folders but skip few

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch delete folders but skip few

Reply to Message Icon

Name: banglabhai
Date: May 31, 2006 at 10:57:39 Pacific
OS: win xp pro
CPU/Ram: Pentium-IV, 256MB
Product: Dell/Dimension GX280
Comment:

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:\temp

Now 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.



Sponsored Link
Ads by Google

Response Number 1
Name: CWoodward
Date: May 31, 2006 at 21:03:56 Pacific
Reply:

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


0

Response Number 2
Name: CWoodward
Date: May 31, 2006 at 21:24:02 Pacific
Reply:

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.


0

Response Number 3
Name: CWoodward
Date: May 31, 2006 at 21:33:19 Pacific
Reply:

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.


0

Response Number 4
Name: banglabhai
Date: June 1, 2006 at 13:06:45 Pacific
Reply:

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.


0

Response Number 5
Name: banglabhai
Date: June 16, 2006 at 08:16:42 Pacific
Reply:

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 from

c:\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.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: June 17, 2006 at 03:03:24 Pacific
Reply:

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



0

Response Number 7
Name: banglabhai
Date: July 13, 2006 at 13:33:17 Pacific
Reply:

::== 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.


0

Response Number 8
Name: Mechanix2Go
Date: July 13, 2006 at 14:51:48 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Crystal Reports and Delph... FOR problems



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch delete folders but skip few

deleting folders - use batch prog www.computing.net/answers/programming/deleting-folders-use-batch-prog/11998.html

Batch delete folder www.computing.net/answers/programming/batch-delete-folder/14481.html

Batch file to delete folder in fol. www.computing.net/answers/programming/batch-file-to-delete-folder-in-fol/16994.html