in reply to Re^2: Search for file ignore case
in thread Search for file ignore case
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.
|
|---|