Solved

delete directories and files that do not match __DATA__

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
Six Sigma Control Plans

705 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