Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Search for file ignore case

by Noame (Beadle)
on May 18, 2008 at 19:20 UTC ( #687244=perlquestion: print w/replies, xml ) Need Help??
Noame has asked for the wisdom of the Perl Monks concerning the following question:

Who can I search for specified file like: <c>TeSt.MaK<\c> under specified directory ignore case? Thanks

Replies are listed 'Best First'.
Re: Search for file ignore case
by ikegami (Pope) on May 18, 2008 at 19:32 UTC

    File::Find allows you to specify your match criteria as Perl code, so you can do a case insensitive match for the file name you want.

    File::Find::Rule allows you to specify your specify a regexp to ->name, and regexps can do case insensitive matches.

    What have you tried?

      I've the file name ( get it from some function), an example: TeSt.Mak

      The file is existing under the directory (e.g: test.mak), but can appear with the same name but mixed case.
      I need to retrieve the correct file name as it appears under the directory.

        In Windows, you can use GetLongPathName in Win32.

        >copy nul test.mak 1 file(s) copied. >perl -le"use Win32; print Win32::GetLongPathName('TeSt.Mak');" test.mak
Re: Search for file ignore case
by mwah (Hermit) on May 18, 2008 at 19:32 UTC

    In the shell, eg. by using du:

    $~>  du -a | egrep -i TEST.MAK

    But there are zillions of other ways too.



      du -a can be replaced by find. That'll eliminate the leading junk.

      i need perl command to find the file under unix account. I've get the file from function, e.g: TeST.maK.

      Once i've the file name i need to look for the correct name under spacifiad directory using ignore case.

      if the corret file is: TEST.mak i need to initalize variable with it.

        The following code shows a function that will receive the directory where to look for the files and the filename you got from function (TeST.maK). It will then read the directory contents and perform a test to see if the filename matches any of the files in the directory. The i modifier on the regexp makes that match case insensitive. If a match is found, the actual name of the file is returned, otherwise the returned value will be an empty string.

        sub getFileName { my $dir = shift; my $file = shift; opendir(DIR, $dir); my @dirContent = readdir(DIR); closedir(DIR); foreach my $content (@dirContent) { if($file =~ /^$content$/i) { # filename matches return $content; } } return ''; }

        note: verbose solution for clarity.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://687244]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2018-02-18 07:07 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.