Solved

delete directories and files that do not match __DATA__

Posted on 2007-04-02
3
176 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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