[tpm] Another Regex question - follow on question

Chris Jones cj at enersave.ca
Tue Oct 23 11:32:41 PDT 2012

My tests are working correctly, I don't try to delete directories.

My follow on question is how do you tell File:Find to skip the 
directory the script is called from.  I only want to delete files in 
the sub-directories.

I can test each file to see if it's directory is '.' but would seem 
somewhat inefficient?

>On Tue, Oct 23, 2012 at 11:41 AM, Antonio Sun <antoniosun at lavabit.com> wrote:
> >
> >>        if (( -f $file) && ( $file =~ /\.(inp|pd2)$/ ))
> >
> > without much formal requirement definition, try to start with this
> >
> >         if (( -f $file) && ( $file =~ /^\w+-\w+\.(inp|pd2)$/ ))
>That just makes the regex even MORE specific, the question is
>why doesn't the original test work?
>I tried:
>perl -e '$file = "SomeFIle-Name.r2.pd2"; if ($file =~ /\.(inp|pd2)$/)
>{ print "found\n"; } else { print "not found\n"}'
>and it worked... just like you'd expect!
>So I suspect that your being caught on the '-f $file' check.
>I didn't look into it, but... are you being caught by the (potential?) problem
>that you are getting just a filename, and not a filespec provided to
>the '-f' test,
>and hence the file isn't found ?

The original regex was finding the files partially correctly.  It was 
finding all files ending with .inp or .pd2.  My problem is that I was 
trying to do too many things with one regex.

The program creates a number of "copies" of the original input files 
and runs those copies as parametric runs.  Those parametric files are 
not removed as they are useful for quality control.

So the original input files might be:

E4 Trenton - Proposed.inp
E4 Trenton - Proposed.pd2

The program will create parametric files:
E4 Trenton - Proposed - CompError4.inp
E4 Trenton - Proposed - CompError4.pd2
E4 Trenton - Proposed - MNECB Reference Building.inp

In addition to a whole bunch of files with extensions not .inp or .pd2.

I have modified the regex to find only .inp or .pd2 files:
if (( -f $file) && ( $file =~ /\w+(\s+|\w+|-)\.(inp|pd2)$/ ))

Then I added a second test to try to eliminate the extraneous files:
sub wanted {
         my $file = $_;
         #mod, eux, cfg test for each one
         if (( -f $file) && ( $file =~ /\w+(\s+|\w+|-)\.(inp|pd2)$/ ))
         #if (( -f $file) && ( $file =~ /\.(inp|pd2)$/ ))
                 if ( $file =~ /(HVAC Sizing|CompError|MNECB Proposed 
Building)\.(inp|pd2)$/ )
                         print "File $file, delete 1\n";
                         print "File $file, keep 1\n";
                 print "File $file, delete 2\n";

The second test is specific enough for my needs except I need to add 
a test to ensure I don't delete directories.

Christopher Jones, P.Eng.
Suite 1801, 1 Yonge Street
Toronto, ON M5E1W7
Tel. 416-203-7465
Fax. 416-946-1005
email cj at enersave.ca

More information about the toronto-pm mailing list