Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to get full path name in windows?

by Anonymous Monk
on Dec 09, 2012 at 17:54 UTC ( [id://1007994]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks
I wrote a code to get the absolute paths of the files in a directory,but I get the path showing the files as present in current directory. Eg: Here, I'm using the directory Cluster1 on the desktop with files doc.txt,volt.txt and holiday.txt. The absolute path should be C:\\Users\\Me\\Desktop\\Cluster1\\doc.txt. But, I get the output as D:\Prog\Perls\doc.txt(D:\Prog\Perls is the location where I execute my code).

$dirname="C:\\Users\\Me\\Desktop\\Cluster1"; opendir(DIR, $dirname); @FILES= readdir(DIR); foreach $FILE (@FILES){ $fil_path = File::Spec->rel2abs($FILE); print "File path:", $fil_path; } closedir DIR;
Can you suggest a way to get the absolute path of the file from its original location.(I would also like the path to have 2 escape characters(\\) as a single one doesn't work while accessing a file.)
Thank you.

Replies are listed 'Best First'.
Re: How to get full path name in windows?
by CountZero (Bishop) on Dec 09, 2012 at 18:52 UTC
    Even on Windows the forward slash in filepaths works. That saves you a lot of escaping.
    use Modern::Perl; my $dirname = 'c:/data/eps'; opendir( DIR, $dirname ); my @files = readdir(DIR); foreach my $file (@files) { my $file_path = "$dirname/$file"; say "File path: $file_path"; } closedir DIR;
    Output
    File path: c:/data/eps/. File path: c:/data/eps/.. File path: c:/data/eps/agrafort.jpg File path: c:/data/eps/alazif File path: c:/data/eps/ARF.jpg File path: c:/data/eps/ARF.png ...
    For anything more complicated than a simple list of all files in a directory, File::Find::Rule is something to think about (and it gives you the full file path).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: How to get full path name in windows?
by ww (Archbishop) on Dec 09, 2012 at 18:14 UTC
    Close...
    #!/usr/bin/perl use 5.014; # use 5.014 is effectivly the same as use +strict. Do it. use File::Spec; # you can't use File::Spec and components +until you've # imported the module and its components my ($FILE, @FILES); # strict requires my; more important, it w +ill tell you about many goofs my $dirname="...

    Update: fixed missing words in comment.

Re: How to get full path name in windows?
by bart (Canon) on Dec 10, 2012 at 12:47 UTC
    If you're just interested in "*.txt" files in one directory, I recommend to forego using readdir and friends, and go straight to glob.
    @txt_files = glob qq("C:\\Users\\Me\\Desktop\\Cluster1\\*.txt");

    Unlike readdir, glob returns the full path in the same format as you passed as an argument: absolute path in yields absolute path out; relative path in (relative to the current working directory of perl which you can change with chdir) yields relative path out.

    Note that if (and only if) your $dirname contains spaces, then the double quotes are required. You don't have to put them around the whole path-with-wildcards, replacing every single space with qq(" ") will work, too.

Re: How to get full path name in windows?
by Tommy (Chaplain) on Dec 09, 2012 at 22:30 UTC

    This works for me:

    use File::Util; my $f = File::Util->new(); my @files = $f->list_dir( "C:\\book", qw( --no-fsdots --with-paths ) ) +;

    From the command line: (demonstrating that "/" works fine on windows)

    $ perl -MFile::Util -e 'print join "\n", File::Util->new->list_dir("C: +/book", qw( --no-fsdots --with-paths ))' C:/book/Generic_User_Guide.pdf C:/book/Quick_Guide.pdf
    --
    Tommy
    $ perl -MMIME::Base64 -e 'print decode_base64 "YWNlQHRvbW15YnV0bGVyLm1lCg=="'
Re: How to get full path name in windows?
by Khen1950fx (Canon) on Dec 09, 2012 at 22:48 UTC
    Using CountZero's script with a regexp:
    #!/usr/bin/perl -l use strict; use warnings; my $dirname = shift @ARGV; opendir( DIR, $dirname ); my(@files) = readdir(DIR); print "File path: "; foreach my $file (@files) { my $file_path = "$dirname/$file"; $file_path =~ s/\\|\//\\\\/g; print $file_path; } closedir DIR;
Re: How to get full path name in windows?
by 2teez (Vicar) on Dec 10, 2012 at 03:59 UTC

    Am adding just a "kobo" suggestion, using your OP and the modification provided by ww to your script.

    Anonymous Monk says:
    "..D:\Prog\Perls is the location where I execute my code.."

    In that case, you can get your desired result using chdir. {Updated}
    Like so:

    #!/usr/bin/perl use warnings; use strict; use File::Spec; my $dirname = "C:\\Users\\Me\\Desktop\\Cluster1"; my @file_to_use_later; ## for use later chdir $dirname; ## add this to OP script opendir DIR, $dirname or die "can't open directory: $!"; my @FILES = readdir(DIR); foreach my $FILE (@FILES) { my $fil_path = File::Spec->rel2abs($FILE); print "File path:", $fil_path, $/; push @file_to_use_later, $fil_path if -f $fil_path; ## check fi +les contents later } closedir DIR or die "can't close directory: $!"; ## test the files stored in the array variable later for my $filename (@file_to_use_later) { print $filename, $/; open my $fh, '<', $filename or die "can't open: $!"; while (<$fh>) { chomp; print $_, $/; } close $fh or die "can't close file:$!"; }

    Also, take note of this important info from File::Spec documentation about the "rel2abs".
    rel2abs() Converts a relative path to an absolute path.
    $abs_path = File::Spec->rel2abs( $path ) ; $abs_path = File::Spec->rel2abs( $path, $base ) ;
    If $base is not present or '', then Cwd is used. If $base is relative, then it is converted to absolute form using rel2abs(). This means that it is taken to be relative to Cwd.

    On systems with the concept of volume, if $path and $base appear to be on two different volumes, we will not attempt to resolve the two paths, and we will instead simply return $path .

    The above explain, why you are getting this:
    D:\Prog\Perls\doc.txt D:\Prog\Perls\volt.txt D:\Prog\Perls\holiday.txt
    instead of this
    C:\Users\Me\Desktop\Cluster1\doc.txt C:\Users\Me\Desktop\Cluster1\volt.txt C:\Users\Me\Desktop\Cluster1\holiday.txt
    Update:
    You may also want to look into the module Path::Class and it's "cousins".
    Path::Class 'just' is a "wrapper" for File::Spec.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1007994]
Approved by ww
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-19 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found