?
Solved

Script or Batch file - Delete all folders EXCEPT ... also Remote Management help

Posted on 2004-11-23
12
Medium Priority
?
8,407 Views
Last Modified: 2013-12-03
I need a script or a batch file that can either be run automatically or manually by us, remotely.  It should do the following to each of 17 computers in a lab.  The computers are running XP Pro SP-2.

1) Restart all of the computers (I know how to do this, and figure I would need two scripts, one to run after the other... how do I set that up?)
2) Inside of a C:\Documents and Settings - Delete all folders except for:
     - All Users
     - Administrator
     - Default User
     - Any other system folders that need to stay in there.

We do not know what the names of the folder would be... otherwise this would be a very simple batch.  Not only can the folders in there be any one of 800 or so users, it may also be variations of that user's login name (such as doejohn.000, doejohn.001, etc.).

I would also need some way of knowing that the folders were successfully deleted.  A single log would be nice, back at my (the remote administrator) computer.  One log file per machine would be not so nice (but workable), and I would need some way of collecting and analyzing all 17 files quickly.

Unfortunately, folder sharing is not a solution for us here.  If this task simply cannot be done, please explain that and why, if you could.

We eventually plan on expanding this to all of the PC's that we manage (200+), and are using this lab simply as a pilot.  Getting this to be as automated as possible is very important.  Actually scheduling the task to run automatically will not be a problem, as I already know how to do this.  It would be best to use the remote management capabilities of XP (see below) but I can also work with putting a script or batch file on each workstation, if need be.

This script would serve as a temporary fix for another issue we are having, detailed here: http://www.experts-exchange.com/Operating_Systems/WinXP/Q_21210077.html.  That issue is still unsolved, is directly related to the Autodesk licensing service, and is worth 500 points.

On a side note... if you can give any suggestions, links, tutorials, etc. on managing remote XP Pro machines, please do so.  We currently use UltraVNC and Remote Desktop on occasion, but I know there is a way to connect to computer management, and I'm sure plenty of other things, using another remote XP Pro machine.  I'm looking for a detailed list or something that goes through all the possibilites, command line or not.  For instance - stopping and starting a service on 100 computers at once, and (hopefully) getting conformation of any failures.

Points will be split if you can answer a piece of the question.

Thanks in advance,
~ Jonny
0
Comment
Question by:jonnyz0109
  • 7
  • 3
  • 2
12 Comments
 
LVL 7

Accepted Solution

by:
tymes earned 1000 total points
ID: 12658817
I told mentioned the  HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\DeleteRoamingCache REG_DWORD and DELPROF.EXE in the other thread, but you can also put this a logon script...


REM for each roaming user directory delete the local
set userprofilesdir="C:\Documents and Settings"
pushd %userprofiledir%
for %%d in ("\\server\userprofiles$\*") do (
 if "%%~nd" neq "%username%" rd /s /q "%userprofiledir%\%%~nd"
)
popd

or

REM for each local directory that isn't current or special delete it
set userprofilesdir="C:\Documents and Settings"
pushd %userprofiledir%
for %%d in (*) do (
 if "%%~fd" neq "%userprofile%" if "%%d" neq "All Users" if "%%d" neq "Default User" if "%%d" neq "LocalSystem" if "%%d" neq "NetworkService" rd /s /q %%d
)
popd

Or I might put a special ".donotdelete" file in each special directory and do a for IF NOT EXIST %D\.donotdelete then delete.
0
 
LVL 4

Author Comment

by:jonnyz0109
ID: 12659303
Can you please show me how to do the second script using the "donotdelete" file?  
0
 
LVL 7

Assisted Solution

by:tymes
tymes earned 1000 total points
ID: 12661178
Oh, I forgot /D between each of the FOR %%d above to iterate through directories instead of files... oops.  Use "FOR /d %%d" in each of my examples above.
so anyways, something like ...

for /d %%d in ("C:\Documents and Settings\*") do if NOT EXIST "%%d\_DO_NOT_DELETE_PROFILE"  echo rd /q /s "%%d"

Create files called _DO_NOT_DELETE_PROFILE in each of the directories that you don't want it to delete (if it doesn't find the file then it will try to delete the directory), this may actually pose a problem if it doesn't have permission to look at that file in which case it will try to delete the directory.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:jonnyz0109
ID: 12667999
Tymes, that sounds good!  Unfortunately I will not be back to the PC labs until Next monday, which is when I can test this out some more.  It'd be great if you can check back and I will let you know how things go.

Thanks for your help in both of these questions!

~ Jonny
0
 
LVL 4

Author Comment

by:jonnyz0109
ID: 12708549
Thank you very much for your help... I am using the second script above without the do not delete file.  We plan on running this batch script on every PC on startup (before anyone logs in) to get rid of all profiles.  I think this should solve our problem!

Thanks again,

Jonny
0
 
LVL 3

Expert Comment

by:adamshields
ID: 13005242
I tried placing the following into a bat file but it didn't run? Any suggestions? Thanks

set userprofilesdir="C:\Documents and Settings"
pushd %userprofiledir%
for %%d in (*) do (
 if "%%d" neq "Administrator" if "%%d" neq "All Users" if "%%d" neq "Default User" if "%%d" neq "LocalSystem" if "%%d" neq "NetworkService" rd /s /q %%d
)
popd
0
 
LVL 4

Author Comment

by:jonnyz0109
ID: 13005453
He posted a second time with a modification to his original code... you're missing the /d flag.  Here is my code for the working script:

echo off
echo. >> "C:\Profile Cleanup\Profile Cleanup.log"
echo -------------------- %time%: SYSTEM STARTUP - PROFILE CLEANUP -------------------- >> "C:\Profile Cleanup\Profile Cleanup.log"
echo Script running as %username%. >> "C:\Profile Cleanup\Profile Cleanup.log"
pushd "C:\Documents and Settings"
for /d %%d in (*) do ( if "%%~fd" neq "%userprofile%" if "%%d" neq "All Users" if "%%d" neq "Default User" if "%%d" neq "LocalService" if "%%d" neq "NetworkService" if "%%d" neq "user" if "%%d" neq "Administrator" rd /s /q "%%d" & echo Deleting directory "%%d" >> "C:\Profile Cleanup\Profile Cleanup.log" )
popd
echo -------------------- %time%: PROFILE CLEANUP COMPLETE -------------------- >> "C:\Profile Cleanup\Profile Cleanup.log"
echo. >> "C:\Profile Cleanup\Profile Cleanup.log"
0
 
LVL 4

Author Comment

by:jonnyz0109
ID: 13005475
the echo's are optional.  I have the script writing to a log file so I can go into each individual machine and see that the script has been working properly, what it's been deleting, etc.
0
 
LVL 3

Expert Comment

by:adamshields
ID: 13005697
That worked well but when I added the script to logoff in gpedit.msc, it only works when the administrator logs off and not reguler users logoff?
0
 
LVL 4

Author Comment

by:jonnyz0109
ID: 13005757
My first thought: It probably needs administrator access to run.  Try setting permissions to everyone?  On second thought: technically when the logoff script executes, you're still logged in, right?  You can't delete a user's directory that is still logged in... This script really needs to be run either a) when one of the "exempt" users is logged in (i.e. "user" for me or "administrator" otherwise), or when nobody is logged in.  For that reason:

What I did was create a process using srvany.exe and I even created a little installer so the techs can easily install the script onto a new XP image (I left the job for now to go back to school).  The process runs at system startup before anybody even logs off.  If this isn't enough to get you started you might want to open a new question... that way you will get multiple experts participating.  Just post a link here if you want tymes and/or me to participate   :o)
0
 
LVL 3

Expert Comment

by:adamshields
ID: 13006263
0
 
LVL 4

Author Comment

by:jonnyz0109
ID: 13006359
I'll watch that thread.  Thanks

~ Jonny  :o)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you unable to synchronize your OST (Offline Storage Table) file with Microsoft Exchange Server? Is your OST file exceeding 2 GB size limit? In Microsoft Outlook 2002 and earlier versions, there is a 2 GB size limit for the OST file. If the file …
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

864 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