Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Fetdching filenames starting numbers.

by Anonymous Monk
on Aug 22, 2002 at 16:17 UTC ( [id://192071]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

On my Windows 2000 I have a search that gets all files and subdirectories but doesnt get files that start with numbers. I have several files that are numbered such as:
001245AG.ppt
Here is my script:
use strict; use File::Find; my $myfile = ">mytext.txt"; open(FILE,"$myfile") || "can not open: $!\n"; my $fil=0; my $dir=0; sub search { next if(($_ != m/^\.+$/) || ($_ != m/^ws\_ftp/i)); { if (!-l && -d) { print FILE "\n\nDIRECTORY\t\t$File::Find::name\n"; print FILE "-----------------------------------------------\n" +; $dir++; } else { print FILE "$_\n"; $fil++; } } } find(\&search, '/mydirectoryname'); print FILE "\n____________________________________\n"; print FILE "\n\nTotal Directory count = $dir.\n"; print FILE "\nTotal File count = $fil.\n"; close(FILE);

Replies are listed 'Best First'.
Re: Fetdching filenames starting numbers.
by fruiture (Curate) on Aug 22, 2002 at 16:37 UTC

    your search() function is quite broken. Let me rewrite a bit of your code and make comments:

    my $myfile = ">mytext.txt"; open(FILE, $myfile ) || "can not open: $!\n"; # please do not quote "$vars" # $ perldoc -q 'quoting.*vars' my ($fil,$dir) = (0,0); sub search { return if /^\.+$/ or /^ws_ftp/i; # i don#t know what the broken syntax ala # ($_ != /../) should mean, but it was nonsense # see perlop for '=~' and perlvar for $_ # the return statement is important # exiting from sub via next is bad and you'll get # warnings if( ! -l and -d _ ){ # the _ filehandle is described in `perldoc -f stat` print FILE "\n\nDIRECTORY\t\t$File::Find::name\n", ("-" x 20) , "\n"; $dir++; } else { print FILE "$_\n"; $fil++ } }

    consider reading about perlstyle, perloperators, perlsyntax and control statements, perlsubroutines, and of course on the 'strict' pragma and on the -w switch (perlrun) and the 'warnings' pragma.

    --
    http://fruiture.de
      I thank you for giving me much needed answer and more knowledge on Perl. I am not familiar with 'return'. What does it do?

        There's a "return" statement in nearly every procedural programming language supporting subroutines. It causes a subroutine to be stopped and to return a certain value to the caller.

        sub foo { return 1 }; $var = foo(); #$var is 1 sub bar { print "AAA"; return 1; print "BBB"; #will never happen, for 'return' has # stopped this sub } $var = bar();

        The breaking character of return is not present in all languages, i know ugly Pascal doesn't have a return statement and you cannot exit a sub from the middle.

        These are the very basics, try a good book to start!

        --
        http://fruiture.de
Re: Fetdching filenames starting numbers.
by Anonymous Monk on Aug 22, 2002 at 16:43 UTC
    Your problem is in the
      next if (($_ != m/^\.+$/) || ($_ != m/^ws\_ftp/i));
    
    line. Get rid of the "$_ !=" in each of them.

    The reason for this has to do with Perl numeric conversion of strings. I believe that what it is doing is applying the match, getting back either a 1 (true) or "" (false). The 1 is a one, no problem in converting it there, but the "" converts to 0.

    Then when it converts your file name (and it does this due to the !=, a numeric operation) "1234.ppt" it gets back 1234, which will not match either a 1 or a 0. For files that don't start with numbers, you're not having the problem because they're all converting to 0.

    I'm a little surprised you haven't gotten any filenames that start with ws_ftp, though. It seems like you should be getting some bad filenames as well as having the others omitted.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-07-18 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.