Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Windows DOS Batch Profile Removal

Posted on 2010-09-21
3
Medium Priority
?
335 Views
Last Modified: 2012-05-10
Hi there
I have found the following code on here which enables me to delete profiles very well. However i have hundreds of machines with duplicate profiles on and if i run this code it deletes the current users profile.
This is because everytime someone logs in to the machine an additional profile gets created and the code does not read this as a current user profile and attempts to delete it.
For example, Person A has a profile on the machine, they restart the machine and log in again, Person A then has a profile called Person A.WorkstationName. The code looks for the current user can't find it and deletes the profile Person A.WorkstationName which is currently being used.
If anyone could offer any advice it would be appreciated.
@echo off
for /f "tokens=*" %%a in ('dir c:\docume~1 /b /ad') do call :process "%%a"
goto :eof

:process
if [%1]==["Administrator"] goto :eof
if [%1]==["All Users"] goto :eof
if [%1]==["Default User"] goto :eof
if [%1]==["LocalService"] goto :eof
if [%1]==["NetworkService"] goto :eof
if [%1]==["Mits"] goto :eof
if [%1]==["Student"] goto :eof
if [%1]==["NWUSERNAME"] goto :eof
if [%1]==["USERNAME"] goto :eof
cd\docume~1
echo. About to remove profile %1
pause
rmdir %1 /s /q

Open in new window

0
Comment
Question by:TPS1979
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 33723774
Does the %userprofile% variable get set correctly for these users?  If so you could add another exception by getting the directory name from the %userprofile% variable using a quick subroutine:

@echo off
call :getdir "%userprofile%"
echo %dirname%


:getdir
  set dirname=%~nx1
exit /b

You can also get the "root" profiles directory, at least based on the current profile so you don't have to hard code c:\docum... I have defined that as %rootdir%.

Have added /i to your directory checks so it makes them case insensitive and a /d to the cd command in case a machine has had the profiles moved to another drive etc.


@echo off
REM Obtain root directory of user profiles and current user profile name
call :getdir "%userprofile%"

REM run down each profile directory and call subroutine
for /f "tokens=*" %%a in ('dir "%rootdir%" /b /ad') do call :process "%%a"
goto :eof

:process
if /i [%1]==["Administrator"] goto :eof
if /i [%1]==["All Users"] goto :eof
if /i [%1]==["Default User"] goto :eof
if /i  [%1]==["LocalService"] goto :eof
if /i  [%1]==["NetworkService"] goto :eof
if /i  [%1]==["Mits"] goto :eof
if /i  [%1]==["Student"] goto :eof
if /i  [%1]==["NWUSERNAME"] goto :eof
if /i  [%1]==["USERNAME"] goto :eof
if /i  [%1]==["%dirname%"] goto :eof

cd /d "%rootdir%"
echo. About to remove profile %1
pause
rmdir %1 /s /q

exit /b

:getdir
  REM takes one parameter and strips the "filename/directoryname" and extension off that and returns it in %dirname%
  set dirname=%~nx1
  set rootdir=%~dp1
exit /b

Steve
0
 

Author Comment

by:TPS1979
ID: 33723966
Very impressed, worked a treat! Thank you.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33724000
No problem.... you just have to know some tricks sometimes.  Lots of info in the help texts of hte commands such as set /? for /? etc. and frankly learned most of the new stuff here over the years!

Of course if some users sets something like their profile path to c:\myname then the above could cause a few issues shall we say.... as in the root profiles dir would then be seen as c:\ ....

Good luck with anyway and just stick a question on here if it causes any issues

Steve
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question