Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
[robby_dobby]: Can I go participate in an YAPC?
[Eily]: (I was trying to find a translation but google translate didn't help)
robby_dobby is not entirely serious
[LanX]: ... thank God France doesn't have an electoral college...
[Eily]: LanX, let's hope it gets better for the second turn
vrk adds oatmeal cookies to the platter on the sideboard.
[LanX]: evrybody can participate in a YAPC
choroba is still undecided what to present at YAPC
[LanX]: imagine the stock market if the choice for the second round would have been those two ...
[robby_dobby]: LanX: Not me. No YAPC in my country and the only one in Asia is so far off in Japan

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-04-24 15:52 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (442 votes). Check out past polls.