Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How to look for two different file extensions?

by Superbroom2.0 (Novice)
on Mar 09, 2017 at 17:22 UTC ( [id://1184063]=perlquestion: print w/replies, xml ) Need Help??

Superbroom2.0 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to pretty much look for either ".xlsx" or ".pdf" file extensions within a directory, and if it finds them, do X, Y, and Z. This works if I just specify ".xlsx" but not the two or more together

while ($filename = readdir(DIR)) # read all entries +in directory { if (-f $filename) { if ($filename ne $SumRptName) # do NOT include th +e summary report file { $FileExt = ""; ($BaseFileName, $FileDir,$FileExt) = fileparse($filename +, qr/\.[^.]*/); print "$FileExt\n"; if ($FileExt eq ".xlsx") + # look for either .xlsx or .pdf || if ($FileExt eq ".pdf") { if (length($FileExt) > 3) { $FileExt = substr($FileExt, 1, 3); $OverrideExt = "/FileType=$FileExt "; } else { $OverrideExt = ""; } if ($FolderPerFile) # add BaseFile +Name to folder path { $Finalfld = $fld . $BaseFileName . "/"; } else { $Finalfld = $fld; }

Replies are listed 'Best First'.
Re: How to look for two different file extensions?
by stevieb (Canon) on Mar 09, 2017 at 17:26 UTC
    if ($FileExt eq ".xlsx") || if ($FileExt eq ".pdf")

    That doesn't do what you're attempting to do. Combine the conditional within the same if() statement:

    if ($FileExt eq '.xlsx' || $FileExt eq '.pdf'){ ... }

      I think I tried that but without the variable:

       if ($FileExt eq '.xlsx' || '.pdf'){

      I guess my thought is that it would say "If the variable for the file extension is this OR this, do this" Thank you for clarifying for me!

        if ($FileExt eq '.xlsx' || '.pdf'){

        Unfortunately that doesn't work, although TIMTOWTDI (There's More Than One Way To Do It):

        if ( $FileExt eq '.xlsx' || $FileExt eq '.pdf' ) { print "Match!\n" } if ( $FileExt =~ /^\.(?:xlsx|pdf)$/ ) { print "Match!\n" } use Quantum::Superpositions; # OR #use Perl6::Junction qw/ all any none one /; if ( $FileExt eq any('.xlsx','.pdf') ) { print "Match!\n" }

        Perl 6 supports Junctions natively, but note that Perl 6 is a related but different language that I wouldn't call production ready yet.

        Update: Added a word.

        A reply falls below the community's threshold of quality. You may see it by logging in.

        A little more - I hope - clarification. The code you have given translates roughly thus: "If the variable evaluates to '.xlsx' or if '.pdf' evaluates to true". IIRC, COBOL - at least some dialects - would do what you mean in this context, but Perl is more powerful and it's quite common for programmers to put in a variable (or even a constant like DEBUG) into a conditional, which is why the compiler doesn't treat your code as you meant it. I remember running into this while taking courses in Pascal and COBOL concurrently - nearly 40 years ago.

        Regards,

        John Davies

        Update: memories came flooding back overnight, including the fact that I have the COBOL for Univac 1100 manual, which says:

        In a compound conditional expression ... the subject may be implied. For example, AGE IS GREATER THAN 21 OR LESS THAN 65 ... Operators, as well as subjects, may be implied ... for example, DISTRICT IS EQUAL TO 25 OR EQUAL TO 66 OR EQUAL TO 85 may be written as, DISTRICT IS EQUAL TO 25 OR 66 OR 85

        The fun came when someone (not I) wrote X NOT EQUAL TO Y OR Z and it took lots of expansion and De Morgan's laws to explain that he needed to replace OR with AND.

Re: How to look for two different file extensions?
by poj (Abbot) on Mar 09, 2017 at 18:57 UTC

    This logic looks odd because if $FileExt is '.pdf' then it's length (4) will always be greater than 3

    if ($FileExt eq ".pdf"){ if (length($FileExt) > 3){

    You haven't shown the whole code but it may help you to reduce the levels of ifs with some early next statements like this ;

    while (my $filename = readdir(DIR)){ next unless (-f $filename); next if ($filename eq $SumRptNam); if ($filename =~ /\.(pdf|xlsx)$/){ my $FileExt = $1; if ($FileExt eq 'pdf'){ # do X to pdf } if ($FileExt eq 'xlsx'){ # do Y to xlxs } # do Z to both } }

    edit : quotes removed from '$SumRptNam'

    poj
Re: How to look for two different file extensions? glob them!
by Discipulus (Canon) on Mar 10, 2017 at 11:54 UTC
    hello Superbroom2.0 and take my welcome to the monastery (even if bit late)!

    I'd go in another way: glob is very useful in these situations:

    my @file_to_process = glob('*.xlsx *.pdf'); die "No file to process!" unless $file_to_process[0]; print scalar @file_to_process, " files found\n"; foreach my $file (@file_to_process){ # your processing here ...

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How to look for two different file extensions?
by Anonymous Monk on Mar 09, 2017 at 18:14 UTC
    use File::Basename; while (<DATA>) { chomp; ($name,undef,$suffix) = fileparse($_,'.xlsx','.pdf'); print "$name$suffix\n" if $suffix; } __DATA__ perl.pl tar.tgz excel.xlsx pdf.pdf ruby.rb
Re: How to look for two different file extensions?
by Anonymous Monk on Mar 10, 2017 at 02:41 UTC

    Hi,

    I think List::Util's any function mat be of interest.

    J.C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1184063]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found