in reply to How to Get Full Path Name on Win32 System

First of all, you should be careful when using File::Find mixing $File::Find::name with starting out from a relative path for the root directory: File::Find chdirs to the directory the file is in, for the callback, so the proper relative path there is in $_, not in $file::Find::name. Of course, it's a whole other ballgame when you start out from absolute paths.

Anyway: use rel2abs as a class method from File::Spec, or as a function from File::Spec::Functions. Try: either of these:

use File::Find; use File::Spec::Functions qw(rel2abs); $\ = "\n"; find \&print_name, "."; sub print_name{ print rel2abs($_) unless $_ eq "." or $_ eq ".."; }
or
use File::Find; use File::Spec::Functions qw(rel2abs); $\ = "\n"; find \&print_name, rel2abs("."); # or, just for ".": # use Cwd; # find \&print_name, cwd; sub print_name{ print $File::Find::name unless $_ eq "." or $_ eq ".."; }

There's a chance the formatting of the full path is still is not exactly the same as you expect from Windows: there could be a mixture of slashes or backslashes, or the case may not the same as the case for the real physical file/directory names. To fix both these issues, you can use GetLongPathName from Win32, which is a standard library on ActivePerl/Windows.

Replies are listed 'Best First'.
Re^2: How to Get Full Path Name on Win32 System
by ack (Deacon) on Mar 12, 2008 at 05:02 UTC

    Thanks, bart. I just tried your first example and it worked just fine. Though I think I like the Cwd approach from FunkyMonk a little better.

    You wrote "...you should be careful when using File::Find mixing $File::Find::name with starting out from a relative path for the root directory: File::Find chdirs to the directory the file is in, for the callback, so the proper relative path there is in $_, not in $file::Find::name. Of course, it's a whole other ballgame when you start out from absolute paths."

    Good info. I appreciate it. I tried quite a variety of 'test cases' and all worked much better, as you note, with absolute paths than relative paths.

    You also noted that:

    "...There's a chance the formatting of the full path is still is not exactly the same as you expect from Windows: there could be a mixture of slashes or backslashes, or the case may not the same as the case for the real physical file/directory names. To fix both these issues, you can use GetLongPathName from Win32, which is a standard library on ActivePerl/Windows."

    There's a nice tutorial on just that point. It has a couple of nice Regex that can be used to go back and forth between the Win32 path separator and the Perl version (forward slash). I haven't tried the GetLongPathName from Win32, but I'll sure check it out. Again, thanks.

    ack Albuquerque, NM
      GetLongPathName returns the case for the file paths as it is physically stored on disk — Windows path file names are case insensitive, but sometimes you want this exactly right.

      Note that there are also a few more related functions in that module, like for converting between long filenames (may contain spaces, can be longer than 8.3) and the 8.3 DOS compatible filenames.

Re^2: How to Get Full Path Name on Win32 System
by krishnoid (Novice) on Aug 28, 2009 at 18:22 UTC
    Win32::GetLongPathName expanded the 8.3 form and fixed capitalization/case, but didn't standardize slash direction. I had to use Win32::GetLongPathName(abs_path $dirname) on both 'c:/docume~1' and 'C:\Documents and Settings' to get them to match.