merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to use the simple if(-f <file name>) to find if a file exists.
However, I have a problem.
I am getting data about the files names from a database where any letters will all be in upper case.
The actual files names may have some but not all of the letters in lower case.
There can be a mixture of upper and lower case letters in each file name.
There is not a problem that there will be two files names that are the same except for the
case of the letters.
Can I use a modified form of the if, if so what is it?
If not what is the best way to find out if the file exists?
I feel there must be a simple solution but so far trials and books have failed!
  • Comment on Finding if files exist when names randomly have letters in both upper and lower cases

Replies are listed 'Best First'.
Re: Finding if files exist when names randomly have letters in both upper and lower cases
by jasonk (Parson) on Jun 22, 2005 at 14:22 UTC

    If your filesystem is not case sensitive, then -f should just work with the upper case names, if your filesystem is case sensitive, and the case from the database doesn't match what is on the filesystem, then you don't have a file with that name.

    If your filesystem is case sensitive, and you still have to do this, you will have to break filename up into parts, search through each directory from the root, and look to see if any of the directories along the way match the one you are looking for (probably by converting the argument and the directories into the same case using uc() or lc().)

    You are likely to get more help here though if you post some of the things you have tried, instead of just asking for someone to do it for you.


    We're not surrounded, we're in a target-rich environment!

      On second thought, this should work as well...

      use File::Glob qw(:globally :nocase); # instead of if(-f $file) { if(<$file>) {

      We're not surrounded, we're in a target-rich environment!
        use File::Glob qw(:globally :nocase); if(<$file>) {
        Thou Shalt Not Use Scalar Glob Unless Thou Understandest Thy Problems Perfectly.

        In layman's terms, your test will work "every other time" mysteriously.

        In real terms, what's happening is that a scalar glob context is established on the first hit, and then reused on the second hit, so you'll get your filename, then undef, then your filename, then undef, then your filename. Ick.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.


        update: And it was also pointed out to me in the CB that this is a readline() call, not a glob() call, which really wouldn't have worked.
      Many thanks for your suggestions.
      I do appreciate your last comment and I would have done so
      had I thought any of my efforts were worthy of posting.
Re: Finding if files exist when names randomly have letters in both upper and lower cases
by Transient (Hermit) on Jun 22, 2005 at 14:17 UTC
    not sure if this is a viable solution for you:
    given a file name "TEST" (from the db), the simple *NIX command ls -l [Tt][Ee][Ss][Tt] would work. There's probably a better way, but that springs to mind off the top of my head.
Re: Finding if files exist when names randomly have letters in both upper and lower cases
by prasadbabu (Prior) on Jun 22, 2005 at 14:23 UTC

    something like this?

    $_ =~ /^$filename$/i ? print "exists" : print "not exists" for (@databasefiles)

    files names from a database where any letters will all be in upper case

    $_ =~ /^\U$filename\E$/ ? print "exists" : print "not exists" for (@databasefiles)

    Prasad