Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Working directory oddity

by ikegami (Patriarch)
on Oct 18, 2007 at 05:33 UTC ( [id://645646]=note: print w/replies, xml ) Need Help??


in reply to Working directory oddity

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.

Tested.

Replies are listed 'Best First'.
Re^2: Working directory oddity
by sg (Pilgrim) on Oct 19, 2007 at 06:02 UTC

    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.

        I was using File::Spec with Cwd. Isn't the purpose of Cwd, especially its realpath, to be "the hiding module" suggested above?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://645646]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found