Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

[Win32] Verify filename (case sensitively)

by syphilis (Chancellor)
on Oct 08, 2012 at 12:05 UTC ( #997801=perlquestion: print w/replies, xml ) Need Help??
syphilis has asked for the wisdom of the Perl Monks concerning the following question:

On MS Windows, what's the simplest way of verifying that a file is named 'Filename.ext' - as opposed to, say, 'FileName.ext' ?

Windows is not case-sensitive, but git is ... and I don't discover that I've made a case-sensitive slip in supplying a filename to the script that commits my changes to a git repo until the last (most inconvenient) moment.
Better, I think, that the script checks right at the start that the filenames I've provided honor case-sensitivity.

  • Comment on [Win32] Verify filename (case sensitively)

Replies are listed 'Best First'.
Re: [Win32] Verify filename (case sensitively)
by Corion (Pope) on Oct 08, 2012 at 12:14 UTC

    Just yesterday, I investigated that in relation to generating a warning on Windows when a module is loaded with the "wrong" case, like use Strict;. I found this (C++) post, which seems to be fairly OK. It does not do an extra stat() call per file, but a call to SHGetFileInfoW. I don't know if that's horribly slow or not.

    The linked subroutine tries to verify all parts of the path, while at least Perl would only be interested in the last part(s) of the name.

      Hi Corion,
      Prompted by the link you gave, I switched to my C:/_32 directory which contains a file named 'switches.txt'. I tried:
      C:\_32>perl -MWin32 -e "print Win32::GetFullPathName('SwitcHes.txt') C:\_32\SwitcHes.txt
      Not much joy with that .. it just reproduces what I gave it.
      C:\_32>perl -MWin32 -e "print Win32::GetLongPathName('SwitcHes.txt') switches.txt
      That looks more promising !!
      I should be able to make use of that in my script without going to too much trouble at all.

      Incidentally, I did see that post of yours ... but didn't get around to actually reading the thread :-(

      Win32::StrictFileNames does that
      $ perl -Mcgi -e 1 $ perl -Mwin32::strictfileNames -e 1 $ perl -Mwin32::strictfileNames -Mcgi -e 1 Can't locate in @INC (@INC contains:... $ perl -Mwin32::strictfileNames -Mcgi -e 1 Can't locate in @INC (@INC contains:...
        Win32::StrictFileNames does that

        That seems to make use of GetLongPathName(), too.
        It won't build successfully for me using mingw - all tests die with the following diagnostic:

        t/7-rmdir.t ... Can't load 'C:\sisyphusion\Win32-StrictFileNames-0.01\blib\arch/ auto/Win32/StrictFileNames/StrictFileNames.dll' for module Win32::StrictFileNames: load_file:Invalid access to memory location at C:/MinGW/perl512/lib/ line 200.
        at t/7-rmdir.t line 8

        Update: And similar errors when building using an MS compiler (MSVC++-7.0) instead of gcc.

        Also, I notice that the author provides, on his own ppm repo, packages for this module only for perls 5.6 and 5.8. (I don't know if that's because they pose a problem with later versions of perl.)

        Does use Win32::StrictFileNames; catch *all* instances of case-mismatch (eg with '-e', 'open') ?

        Looks like an interesting module to re-visit if I get the time (before I forget all about it :-).
        For the moment, the Win32::GetLongPathName() function will suffice.

        And interesting that, going by your example, the module itself works irrespective of the case that's used when loading it.

Re: [Win32] Verify filename (case sensitively)
by BrowserUk (Pope) on Oct 08, 2012 at 12:37 UTC

    print "$_ ::> ", Win32::GetLongPathName($_) for "\\program files", "\\program files\\cOmmon fiLes", "/pRoGRam + fIlEs (X86)/InStAlLsHiElD iNsTaLlAtIoN iNfORmAtIoN";; \program files ::> \Program Files \program files\cOmmon fiLes ::> \Program Files\Common Files /pRoGRam fIlEs (X86)/InStAlLsHiElD iNsTaLlAtIoN iNfORmAtIoN ::> /Prog +ram Files (x86)/InstallShield Installation Information

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: [Win32] Verify filename (case sensitively)
by rpnoble419 (Pilgrim) on Oct 09, 2012 at 04:28 UTC

    When I need to be cross platform compatible, I always use the lc() function on all of my file name vars. This way all of my files created by my applications are written with lower case characters. If I have to deal with client supplied files, I rename then to lower case and I take out the spaces under Windows.

    my $filename=lc('NaMeoFfiLe.txt');

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997801]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2017-02-24 16:17 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (356 votes). Check out past polls.