Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to Get Full Path Name on Win32 System

by ack (Deacon)
on Mar 11, 2008 at 21:18 UTC ( #673618=perlquestion: print w/ replies, xml ) Need Help??
ack has asked for the wisdom of the Perl Monks concerning the following question:

I was just reading Beginners guide to File::Find in the Monastary Tutorials (and the pod documentation for File::Find) and was testing some of the examples.

The code is simple and is as follows:

#!/user/bin/perl use File::Find; find(\&print_name, "."); exit(0); sub print_name{ print "[$File::Find::dir] [$File::Find::name]\n" unless(($_ eq ".") or ($_ eq "..")) }

The output is:

[.] [.] [./DAYS2MDH.M] [.] [./INVJDAY.M] [.] [./JDAY.M] [.] [./mdy2doy.m] [.] [./MyBigTest.pl] [.] [./MyBigTest.vpd] [.] [./MyLearn04.pl]

According to the Tutorial and the comments, this should work on a Win32 system (which is what I was testing this on). But rather than giving me the 'full path', it gives me '.'.

I seem to recall that on Win32 systems there can be problems with getting full path names; but the Tutorial says otherwise.

How does one get the 'full path' name rather than the '.'? Or just point me in the right direction to address this issue since I can't remember where I saw/read the caveats about path names re: Win32 system?

UPDATE: Thanks everyone. I went with the full directory approach in the find() and it works just fine. In scouting around CPAN I also found File::Find::Wanted which is a real nice, simple wrapper around File::Find and is a bit more intuitive than File::Find. Though it's not clear how one gets to the variables $File::Find::name, $File::Find::dir...which are really nifty in the File::Find module.

ack Albuquerque, NM

Comment on How to Get Full Path Name on Win32 System
Select or Download Code
Re: How to Get Full Path Name on Win32 System
by FunkyMonk (Canon) on Mar 11, 2008 at 21:26 UTC
    Either use the full path instead of ".", or use Cwd (current working directory) to find the full path of ".":
    find(\&print_name, "c:/path/to/wherever"); #or use Cwd; find(\&print_name, cwd);

      Thanks, FunkyMonk. CWD works great with File::Find::find().

      ack Albuquerque, NM
Re: How to Get Full Path Name on Win32 System
by Corion (Pope) on Mar 11, 2008 at 21:37 UTC

      Looks interesting. I'll check it out. Thanks, Corion.

      ack Albuquerque, NM
Re: How to Get Full Path Name on Win32 System
by bart (Canon) on Mar 11, 2008 at 23:05 UTC
    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.

      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.

      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.
Re: How to Get Full Path Name on Win32 System
by Anonymous Monk on Mar 12, 2008 at 03:28 UTC
    File::Find pod says "complete" path ... vague

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://673618]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-08-30 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls