in reply to Re: Search for file ignore case
in thread Search for file ignore case


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.

Replies are listed 'Best First'.
Re^3: Search for file ignore case
by olus (Curate) on May 19, 2008 at 13:58 UTC

    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.