Solved

delete directories and files that do not match __DATA__

Posted on 2007-04-02
3
180 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
[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
3 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 18838094
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
ID: 18838780
Thanks!  
needed a chomp($_); but works great!
0
 
LVL 84

Expert Comment

by:ozo
ID: 18839393
$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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

738 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