moderated >> Cleaning directory trees of unwanted files

by Leon Heller » Mon, 02 Aug 2004 02:29:25 GMT

I couldn't find a PERL program to remove unwanted files from a directory
tree, like the one I use for my PCB designs with each design in a separate
sub-directory, which ends up cluttered with unwanted Backup, Security and
Report files. I therefore wrote my own, based on a directory listing program
I found. It might be useful to someone. I very rarely use PERL and it
probably shows. 8-)

#!/usr/bin/perl

use File::Find;
use strict;

my $directory = "c:/pcb";

find (\&process, $directory);

sub process
{
# Find files for deleting
if ( ($File::Find::name =~ /\bBackup\b/) or
($File::Find::name =~ /\bSecurity\b/) or
($File::Find::name =~ /\.txt$/) )
{
print $File::Find::name;
print "\n";
unlink $File::Find::name;
}
}

The Pulsonix software I use creates Backup and Security files with names
starting with "Backup Copy of" and "Security Copy of" respectively and
report files all have a .txt extension.

Leon
--
Leon Heller, G1HSM
http://www.geocities.com/leon_heller


Similar Threads

1. Text Print Entire Directory Tree including File Names from a FTP Directory

2. Getting all directories/files from current directory and using -d flag for the directories - Perl

3. portable directory tree searching

Been to CPAN enough that I'm seeing crosseyed.

What I want to do is to write a program that will return a list of files named *.edi which are found in <some-directory-base>/*/files_in directory and then process each file one at a time.

Now for the kicker.  It needs to run now on a Win32 platform, and in a few months on a Linux platform with as few changes as possible.

In a linux shell script, it would look something like this:

for file in $base_dir/*/files_in/*.edi
do
    <process $file>
done

Am I looking for a module to return file names when it is a coding issue?

Thanx!

-Michael

4. Finding directories within a tree - Perl

5. "tree" view of directory

  I'd like to have a tree like listing of a directories contents and it's
subdirectories. I'd like to do that with the modules from the standard perl
distribution.

  It doesn't appear that I can do that with File::Find. I'm also unsure of
how to sort a readdir so that it lists directories first.

  Surely this (tree view) has been done many times already!

  I'm working on something like this:

(this doesn't work, it recurses back up and then forever)

sub readADir{
my $dir=shift;
print '<ul>';

opendir DIR, $dir or die "Cannot open dir $!";
my @files = readdir DIR;
closedir DIR;

foreach my $file(@files){
# my @regular_files=();my @directories=();
if(-d "$dir/$file"){unless($file=~/^\./){push @directories, "$dir/$file"}}
if(-f "$dir/$file"){push @regular_files, $file;}
}

@directories=sort @directories;
foreach my $directory(@directories){
if($dir ne $directory){readADir($directory)};
}

@regular_files=sort @regular_files;
foreach my $regular_file(@regular_files){
print qq{<li> $regular_file </li>};
}
print '</ul>';
}

  Jeff


6. Tree view of directory while using Subversion - Perl

7. Probably a FAQ, recursivly creating a directory tree?

Givne a string containing a fullu qualified directory path name, I need a
routine that will verify it's existence, and atempt to create any missing
parts of it.

Surely someone has invented this wheel?


-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin

8. translating an OS directory recursively into a tree object - Perl