Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to check if file exists case insensitive

Posted on 2009-04-16
10
Medium Priority
?
3,912 Views
Last Modified: 2012-05-06
Hello,
I usually check if file exist with
(-e $filename) or die "$filename does not exist\n";
Windows and Mac OS X have case-insensitive file system, but this check is case sensitive. So if $filename='test.TXT', my code return false even if test.txt file exists.
I would like not to use any packages, only standard Perl to be sure it will run on any computer without installing additional packages.
What I can do? There should be an easy solution, but I could not find a good one.
I saw some recommended to do it with ls and grep, but it looks wierd. I cannot believe Perl does not deal with this issue.
Any ideas?
Thanks
0
Comment
Question by:yuk99
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 24158671
(grep /$filename/i <*>) or die "$filename does not exist\n";
0
 
LVL 15

Author Comment

by:yuk99
ID: 24159226
Adam314, thanks.
What <*> actually means for grep?
I got an error on both Windows and Mac:
Not enough arguments for grep at test.pl line 7, near "*>)  "

On Win Perl 5.10.0, on Mac - 5.8.6
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24159441
just change $filename to lowercase  and do the test


-f lc($filename) && print "file exists";

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Author Comment

by:yuk99
ID: 24159571
OK, I found my mistake and it was stupid. I didn't specify the whole path in $filename.
So my original code works under Mac and Windows.

But the problem is still exists now on Unix with case-sensitive file system. I want to eliminate the possibility that case has been changed.

ghostdog74
Your code does not work. It changes the variable name to lower case, but the file can be in upper or mixed case.
0
 
LVL 15

Author Comment

by:yuk99
ID: 24160228
Finally I've go it working. The code is below.
(grep {/$file/i} <$dir/*>) or die "$file not exist in $dir\n";

Open in new window

0
 
LVL 15

Author Closing Comment

by:yuk99
ID: 31571024
Adam314,
Thanks a lot for the hint!
0
 
LVL 85

Expert Comment

by:ozo
ID: 24161483
grep /^\Q$filename\E$/i ,<*> or die "$filename does not exist\n";
0
 
LVL 85

Expert Comment

by:ozo
ID: 24161505
otherwise
 $filename='test.TXT'
may match on
dir/__test_TXT__
and
$filename='test)TXT'
would cause an error
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24164024
>>Your code does not work. It changes the variable name to lower case, but the file can be in upper or mixed case.

i thought you wanted to check case-insensitive. ie Test.txt and test.txt both are allowed.
0
 
LVL 15

Author Comment

by:yuk99
ID: 24164401
ozo,
thanks for the comment. Although it's seems to be quite impossible situation in my case, little precaution won't harm.

ghostdog74,
You are right. Any case allowed - text.txt or Text.TXT should match. But converting just variable to lower case will not help.

Thanks again everybody. I've learned a lot.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question