Link to home
Start Free TrialLog in
Avatar of jonnyz0109
jonnyz0109

asked on

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

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: https://www.experts-exchange.com/questions/21210077/File-is-in-use-by-another-process-How-can-I-find-what-is-using-locking-it.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
ASKER CERTIFIED SOLUTION
Avatar of tymes
tymes
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jonnyz0109
jonnyz0109

ASKER

Can you please show me how to do the second script using the "donotdelete" file?  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
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"
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.
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?
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)
I'll watch that thread.  Thanks

~ Jonny  :o)