# Solved: batch file - delete folders excluding %username%



## indieman (Jul 7, 2010)

Hi,

I am trying to create a batch file that does a bit of spring cleaning for several PCs.

I want it to delete all local user profiles except Administrator, All Users and the user that is logged in at the moment the script is run. Assuming jsmith is logged in the script should:

Delete the following folders and all subfolders and files. There are other profiles not listed below that also need deleting without the need to specify them.
C:\Documents and Settings\fbrown\*
C:\Documents and Settings\apotter\*
C:\Documents and Settings\rtrouter\*
C:\Documents and Settings\lpowell\*

not delete
C:\Documents and Settings\Administrator
C:\Documents and Settings\All Users
C:\Documents and Settings\jsmith

Please can someone offer some advice.

Thanks,


----------



## indieman (Jul 7, 2010)

I have this so far (manually putting in the current user), but cant get this to work either.

@echo off
FOR /D %%i IN ("D:\test\test\*") DO (
FOR %%i NOT IN (Administrator jsmith) DO rmdir /s/q %%i
)


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!
You'll need to exclude *Default User*, *LocalService* and *NetworkService* as well
Remove the *Echo.* to actually delete the folders.

```
@Echo Off
Set _Profiles=%SystemDrive%\Documents and Settings
:: Delete Profile folders that are not specified
PushD "%_Profiles%"
If /I Not "%CD%"=="%_Profiles%" (Echo.Unable to find %_Profiles%, exiting)&Pause&Goto :EOF
For /F "Delims=" %%I In ('Dir /AD /B ^|Findstr /I /V /C:"Administrator" /C:"All Users" /C:"Default User" /C:"LocalService" /C:"NetworkService" /C:"%UserName%"') Do [COLOR=Red][B]Echo.[/B][/COLOR]RD /Q /S "%%I"
PopD
```


----------



## indieman (Jul 7, 2010)

wow, that worked flawlessy. thankyou!


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome!

Noticed I put *Default* instead of *Default User*, so I corrected that above, otherwise it would exclude any account that had Default in it, like JohnDefault.

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------



## indieman (Jul 7, 2010)

I found the default user discrepancy and sorted that. Ive added some code to delete temp files and internet files and then kick off a defrag. all is well.

thank you


----------



## antmar904 (Feb 22, 2010)

Or you can use delprof from Microsoft in a batch! 

http://www.microsoft.com/downloads/...95-6063-4462-8150-360394e98e1e&displaylang=en


----------



## Squashman (Apr 4, 2003)

That is interesting.


----------

