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

Hello Monks
I was scanning for directory for a file name...the file can be
uppercase or lowercase or mixed (upper and lower)..
to tackle i am changing the search criteria to upper case
and search where as in folder its either lower case
or mixed filenames and search is failing how to tackle
this problem any help in this greatly appreciated.
Please find below code which I am using...
my @directory = ("C:\Testing"); my $srcfile; my @sourcefiles; $datfile = uc(test_file_name.dat); find(\&sourcefind, @directory); sub sourcefind { open(FL, $File::Find::name); $sourcefile = $_ if -f and /$datfile/ ; close(FL); } the program has to find successful match if the filename is following Case 1 : test_file_name.dat (all lower case) Case 2 : TEST_FILE_NAME.DAT (all upper case) Case 3 : Test_File_Name.dat (Mixed)

Thanks & Regards
Sridhar

Replies are listed 'Best First'.
Re: File::Find problem
by runrig (Abbot) on Oct 12, 2006 at 21:19 UTC
    Either uc the sourcefile name also, or use a case insensitive match (see /i modifier in perlre). But the way your match currently is (maybe you could just use the 'eq' operator?), you would also mistakenly match not just "test_file_name.dat", but also "my_test_file_name.dat" . That is, if your program didn't have a couple of other mistakes. Print the directory and the datfile variables to see what's wrong (hint: backslashes escape characters, and strings should be quoted...also, you can use forward slashes for path separators in Windows unless you are passing the value to a system function). Also consider using strict and warnings.
Re: File::Find problem
by blazar (Canon) on Oct 12, 2006 at 21:28 UTC
    the program has to find successful match if the filename is following Case 1 : test_file_name.dat (all lower case) Case 2 : TEST_FILE_NAME.DAT (all upper case) Case 3 : Test_File_Name.dat (Mixed)

    Only those three combinations or also the many possible other ones like tESt_FilE_nAMe.DaT? (Even though they may not actually be there.) If the latter, than just use

    lc $name eq 'test_file_name.dat'

    as test.

    Update: not directly related to your problem but also:

    $datfile = uc(test_file_name.dat);
    use strict; # and use warnings; # as well
    open(FL, $File::Find::name);

    Just the usual recommendations:

    1. use the three args form of open;
    2. use lexical filehandles;
    3. check the return value of opens.

    Why are you opening it anyway? (Since you do not actually use it.)

Re: File::Find problem (\)
by tye (Sage) on Oct 12, 2006 at 21:42 UTC
    my @directory = ("C:\Testi­ng");

    That is "C:Testing". If you'd written "C:\testing", then you'd have a tab in that file name. You should double your backslashes in Perl string literals.

    - tye        

Re: File::Find problem
by GrandFather (Saint) on Oct 12, 2006 at 21:19 UTC

    assuming Windows or Mac file systems (case insensitive) all you need to do is make your regex ignore case:

    $sourcefile = $_ if -f and /$datfile/i ;

    Note the 'i', ignore case flag, following the regex.


    DWIM is Perl's answer to Gödel
Re: File::Find problem
by caelifer (Scribe) on Oct 12, 2006 at 23:24 UTC
    Besides all of the above mentioned issues with your code, you may also take a close look at this line
    $sourcefile = $_ if -f and /$datfile/;
    If your intent was to add new file to @sourcefiles array, it would not work. You should use...
    push(@sourcefiles, $_) if -f and /$datafile/i;
    ...instead.

    If, on the other hand, you wanted to slurp the content of the $File::Find::name file into a variable, then you should use something like this:

    open(my $fh, '<', $File::Find::name) or die "Can't open $File::Find::n +ame: $!"; my $source = do { local $/; <$fh> }; close $fh;
    BR
Re: File::Find problem
by mantra2006 (Hermit) on Oct 13, 2006 at 00:08 UTC
    Hello Monks
    Thanks for all the replies to the question...I will modify the
    program as per the suggestions and try to achive the goal(matching filename)..its
    the entire filename match i am interested in...
    Once again thanks for the replies :)

    Thanks & Regards
    Sridhar