Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How do I remove current path from a dir listing

by Fishcake2000 (Initiate)
on Oct 06, 2000 at 13:35 UTC ( [id://35552]=perlquestion: print w/replies, xml ) Need Help??

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

I need to some code that will get the current directory and then remove it from a directory listing that includes sub directorys. i.e:

current dir:
c:\perl
dir /s listing:
c:\perl\bin\aa.exe c:\perl\doc\bb.txt c:\perl\web\index.html
I need:
bin\aa.exe doc\bb.txt web\index.html
current unworking code:
open(GETDIR, "cd |"); while(<GETDIR>) { chomp; $currentDir = $_; } open(DODIR, "dir /b /s|"); while(<DODIR>) { chomp; $pathRemoved = $_; $pathRemoved =~ s/$currentDir//i; print $pathRemoved; }

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I remove current path from a dir listing
by tye (Sage) on Oct 06, 2000 at 19:05 UTC

    I strongly recommend using File::Spec:

    use File::Spec; $rel_path= File::Spec->abs2rel( $abs_path );
    or
    use File::Spec::Functions qw( abs2rel ); $rel_path= abs2rel( $abs_path );
    You can also provide a second argument to abs2rel() that tells it to strip a directory other than the current working directory.

    See also my review of File::Spec.

            - tye
Re: How do I remove current path from a dir listing
by t0mas (Priest) on Oct 06, 2000 at 16:07 UTC
    use strict; use File::Find; use Cwd; my $startDir=getcwd; find(sub {printf "%s\n", substr((split($startDir,$File::Find::name))[1 +],1) if ($File::Find::name ne $startDir);}, $startDir);


    /brother t0mas

Log In?
Username:
Password:

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

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

    No recent polls found