Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Working directory oddity

by teslafreak (Initiate)
on Oct 18, 2007 at 04:32 UTC ( #645641=perlquestion: print w/replies, xml ) Need Help??
teslafreak has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a program where I needed to implement a system to handle directory changes, and actually change the working directory to match. It works great but, upon changing to a new directory, the folders in the path are not properly capitalized. I have even tried using Cwd from the new directory, but it returns the directory exectly the way it was typed in (in all lowercase). I realize that this is purely aesthetic, since the rest of it is working, but I wondered if anyone has any ideas.

Replies are listed 'Best First'.
Re: Working directory oddity
by ikegami (Pope) on Oct 18, 2007 at 05:33 UTC

    Win32's GetFullPathName and GetLongPathName will do that.
    The former also converts the path to an absolute path.
    The latter also expands short file names into long file names.

    use Win32 qw( ); # Win32 doesn't export these, so we'll import them manually. BEGIN { no strict 'refs'; *$_ = \&{"Win32::$_"} for qw( GetLongPathName GetFullPathName ); } my $path = "\\DOCUMENTS AND SETTINGS"; # If you wish to keep relative paths relative: print GetLongPathName($path); # If you wish to make relative paths absolute: print GetLongPathName(scalar(GetFullPathName($path)));

    Note that Win32's GetCwd returns the path as provided to chdir.


      I ran into a similar problem recently. I believe the issue mentioned in the original post "but it returns the directory exectly the way it was typed" is referred to by Microsoft as the feature of being "case insensitive but case preserving". By "case preserving" they do not seem to mean that they preserve the case of the file (or directory) to the case that was used when it was created but that they preserve the case as was used when it was accessed; this is a weird feature.

      The "use Win32" solution means that one needs to branch the user-script based on the OS. I tried to solve the problem using

      use Cwd qw(getcwd chdir realpath); use File::Spec::Functions; # canonpath

      but was not successful -- even canonpath(realpath()) respects the "case preserving" "feature".

        Using the same path you provided avoids confusion. Unix usually does the same thing. If you use /home/ikegami/symlink/dir, it won't magically become /some/where/else/dir.

        File::Spec definitely won't help, since it doesn't do any system calls.

        You could always do a directory listing for every component of the path and look for the matching name. That can fail due to permissions and if one of the components is an 8.3 name, but you can always fall back to what you were provided for those components.

        Finally, so what if you have to branch. You're asking to do something platform-specific, after all. Just hide it in a module with a thin, portable interface. It can call Win32::GetLongPathName on Windows and File::Spec->canonpath on other platforms.

Re: Working directory oddity
by cLive ;-) (Prior) on Oct 18, 2007 at 04:51 UTC

    Windows, right? It helps if you clarify when writing these questions :)

    Possibly abs_path can help?

    use strict; use warnings; use Cwd qw(abs_path); chdir("C:\WINDOWS\PROGRAM FILES"); # <- or whatever it is in windows. my $thisdir = abs_path('.'); print "This dir is $this_dir\n";

    Untested (I'm in Linux), but that would be my first stab at this.

      Sorry, that doesn't work :( and not just cause your slashes are unescaped ;)
      getcwd, cwd and abs_path all return paths using the same caps provided to chdir (and with the slashes reversed to /).

Re: Working directory oddity
by teslafreak (Initiate) on Oct 20, 2007 at 03:15 UTC
    I thought that I may just be missing something obvious. I'm glad to see I wasn't though, makes me feel better about not turning up anything in my searches. Thanks for all the input

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-06-19 19:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.