Solved

delete directories and files that do not match __DATA__

Posted on 2007-04-02
3
174 Views
Last Modified: 2010-03-05
# delete files and directories that do not match __DATA__

# When terminal servers boot, I don't want them to have left
# over roaming user profiles in the c:\documents and settings folder
# but I want to preserve existing administrative account directories
# can you help?

opendir(DIR, $path1);
@files = readdir(DIR);
closedir(DIR);

$path1 = "c:\\documents and settings";
chdir("$path1");

foreach $file (@files) {
      #!?!? not sure what to do.
}

# below are the directories I want to keep, if present
__DATA__
backupadministrator
administrator
all users
default user

0
Comment
Question by:Marketing_Insists
3 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
Comment Utility
my %KeepDirs;
while(<DATA>){
      $KeepDirs{$_}=1;
}

$path1 = "c:\\documents and settings";

chdir("$path1") or die "Could not chdir: $!\n";

opendir(DIR, $path1);
@files = readdir(DIR);
closedir(DIR);


foreach $file (@files) {
      next if $file =~ /^\.{1,2}$/;     #Skip "." and ".." directories
      next if $KeepDirs{$file};         #Skip directories you want to keep
      print "Remove the directory '$file'\n";
      system("echo rmdir /s $file");    #Remove the "echo" to actually remove the directory... this cannot be undone, make sure it is what you want first
}

# below are the directories I want to keep, if present
__DATA__
backupadministrator
administrator
all users
default user


0
 

Author Comment

by:Marketing_Insists
Comment Utility
Thanks!  
needed a chomp($_); but works great!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
$path1 = "c:\\documents and settings";

chdir("$path1") or die "$path1 $!";
while( <DATA> ){
  chomp;
  ++$KeepDirs{$_};
}

use File::Path;
rmtree([grep !$KeepDirs{$_},<*>],1,0);

# below are the directories I want to keep, if present
__DATA__
backupadministrator
administrator
all users
default user
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now