Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do I change .. to the real filename?

by dcardamo (Initiate)
on Jan 22, 2001 at 08:09 UTC ( [id://53416]=perlquestion: print w/replies, xml ) Need Help??

dcardamo has asked for the wisdom of the Perl Monks concerning the following question: (regular expressions)

I have a filename with the entire path '/home/projects/..' The '..' means to go back a directory, so its real path is '/home' Can this be done with a regex or do I have to split this?

Originally posted as a Categorized Question.

  • Comment on How do I change .. to the real filename?

Replies are listed 'Best First'.
Re: How do I change .. to the real filename?
by merlyn (Sage) on Jan 22, 2001 at 08:13 UTC
    use File::Spec; print File::Spec->rel2abs("/home/projects/..");
Re: How do I change .. to the real filename?
by AltBlue (Chaplain) on Jan 27, 2001 at 03:01 UTC
    use Cwd 'abs_path'; print abs_path('/home/projects/..');
Re: How do I change .. to the real filename?
by matthewm (Initiate) on Jan 25, 2001 at 21:01 UTC
    To answer your specifc example..
    $file =~ s%/[^/]+/\.\.$%%
    ... will eat the last directory name that's up'ed by the trailing ..
    To give a more general/complete example that can handle multiple .'s and ..'s at any point in the path.
    # colapse /./ and /.$ 1 while ($file =~ s%/\.(/|$)%$1%g); # colapse /dir/../ and /dir/..$ # ignore /../../ and /../..$ 1 while ($file =~ s%/[^/]+/(?<!/\.\./)\.\.(/|$)%$1%g); # remove ^dir/../ and ^dir/../ # ignore ^../../ and ^../.. 1 while ($file =~ s%^[^/]+/(?<!^\.\./)\.\.(/|$)%%); # colapse ^/../ to / 1 while ($file =~ s%^/\.\./%/%); # expand null to . $file =~ s%^$%.%;
    We need the itterations of each loop as each substitution may reveal additional matchs.
    We need multiple expressions to get the order of susbistion right. That is /../../ should colapse the two proceding directoires and not itself.
    I'm sure it can be reduced more than that but it is as simple as I can see it can be and still catch all the special cases.
    I use something very similar to this at the beginning of my scripts to get the conical path for $0 and not the called path. Although spliting the path into its components and working backwards is probably easier/clearer than using a regex.
Re: How do I change .. to the real filename?
by dcardamo (Initiate) on Jan 23, 2001 at 02:30 UTC
    I tried the first solution, and it didn't work properly. I haven't tried the second, but here is a non-regex routine that I wrote to do that:
    sub relpath($) { my $file = shift; my $newfile = ""; $file =~ s/\n//g; # remove new lines my @peices = split /\//, $file; for (my $i = 0; $i <= $#peices; $i++) { if ($peices[$i + 1] eq "..") { last; } if ($peices[$i] ne ".") { $newfile .= "/$peices[$i]"; } } $newfile =~ s/\/\//\//g; return $newfile; }
Re: How do I change .. to the real filename?
by Shakya (Initiate) on Oct 06, 2007 at 18:59 UTC
    You can even try this -
    1 while ($string =~ s!(/(?:[^/.]+/)*)((?:[^/.]+/)+\.\./?)(.*)$!$1$3!g) +;
Re: How do I change .. to the real filename?
by Gloom (Monk) on Jan 22, 2001 at 22:45 UTC
    Or maybe something like this ?
    $filename =~ s/\/\w+\/\.\.//g;

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-30 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found