Solved

delete directories and files that do not match __DATA__

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…

790 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