CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
translating relative pathname to absolute path

 



ljfaraci
Deleted

Jan 26, 2000, 12:09 PM

Post #1 of 10 (1721 views)
translating relative pathname to absolute path Can't Post

Is there a function to translate a relative DOS (Win32) pathname to the absolute pathname? I'm passing a relative pathname to my perl script and would like to translate it to the absolute name without having to parse it all out if possible...

Thx... LJF...


brian.hayes
User

Jan 27, 2000, 4:53 PM

Post #2 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

Ok. Im not much on terminalogy but I think you are talking about relative as in:
../../yourfile.txt.

and absolute pathname:

c:\$hidden\input\yourfile.txt

Some thing like this?
Could you explain a little more on what you are tring to do please.

Thanks,

Brian Hayes


69_vette
Deleted

Jan 28, 2000, 4:10 AM

Post #3 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

You might want to try swapping certain things out using the tr/// operator.

Something like:

Change the \'s to /'s

$path =~ tr/\\/\//;

Change c:\whatever to /usr/whatever

$path =~ tr/c\:\\whatever/\/usr\/whatever/;

PLEASE correct me if I'm wrong.

I'm just learning this stuff myself...


ljfaraci
Deleted

Jan 28, 2000, 6:28 AM

Post #4 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

An example.

Say I'm in directory "C:\dir1\dir2\dir3\dir4".
I pass to my script a relative pathname: "..\..\newdir"
which would translate to "C:\dir1\dir2\newdir"

I want to know if there is a Perl function I can pass
"..\..\newdir", and have it return "C:\dir1\dir2\newdir"

Thx... LJF...


69_vette
Deleted

Jan 28, 2000, 4:24 PM

Post #5 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

I'm sorry the tr/// operator translates characters... the s/// one would be better...

And I'm sure I got some other stuff wrong too...


japhy
Enthusiast

Jan 28, 2000, 6:39 PM

Post #6 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

I think there is a module to do this, but I don't know its name.

For Windows, it's as easy as string manipulation, because there aren't symbolic links (directories which take you to other directories, really):

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# Perl and Windows can understand /'s
$path = "c:/foo/this/that/../../those/../mine";

1 while $path =~ s{ [^/]+ /\.\./ }{}xg;
</pre><HR></BLOCKQUOTE>

The regex removes directory names followed by the .. directory. You need to do this as a while-loop, because after it's removed "that/../", it's too far ahead to see that the string now holds "this/../".

If you're interested in seeing a Unix solution (that honors symlinks), I'll post one, but only after I think it through and stress test it.


brian.hayes
User

Jan 28, 2000, 8:15 PM

Post #7 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

I'm going to step out on a limb and say allowing a script to be passed /../../ type inputs can be very dangerous....

You may want to consider someother solution??

Example:

I pass to your script based on what you told me.

../../../../windows/hosts

I will now be at your hosts file. So what ever your scipt does to the direction it receives from the input, well you get the idea..

But to give you a lead on what your looking for check out your modules:
If you chose the default install it will be in

C:\perl\site\lib\.. directory.

URI::file
Win32api File.pm This one is in you win32api directory.

Plus it might help us help you if you could post a sample of the code your trying to use.
Brian Hayes


brian.hayes
User

Jan 28, 2000, 8:29 PM

Post #8 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

Also, Try cpan.org. and look for the File::Stat module.


japhy
Enthusiast

Jan 29, 2000, 10:57 AM

Post #9 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

Ah, it was File::Stat, I guess.

Oh, and yes, my regex was a bit fubared. I should probably have done:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$path =~ s{ /\./ }{ / }gx;
1 while $path =~ s{ (?!\.\.) [^/]+ / \.\. / }{}gx;
$path =~ s{ ^(/\.\.)+ }{}gx;
</pre><HR></BLOCKQUOTE>

This hasn't been as stress-tested as it should be. But this time the first regex will remove all references to ./, which is just the current directory. The second regex removes all directories (that AREN'T ../) that are followed by a ../ (and it removes the ../ too). The final regex removes any of the leading /.. paths, because you can't go higher up than / on any system.

Let me know if you find any serious shortcomings.


ljfaraci
Deleted

Jan 31, 2000, 7:20 AM

Post #10 of 10 (1721 views)
Re: translating relative pathname to absolute path [In reply to] Can't Post

I would prefer to pass the directory path that the
user enters to a perl function and have it return
the absolute pathname, than to have to examine it,
and generate the pathname myself.
Thanks for all your suggestions... LJF...

Below is the test program I've currently
developed to do it. Suggestions are always welcome...

## C:\Perl\V500502\perl.exe -w C:\Perl\LJF\testit.pl
use File::Spec;

print "\n\n\nEnter directory: "; # Get target directory from user
$dne = <STDIN>;
chomp ($dne);

# Verify user entered a valid directory path
# and that it's not an absolute pathname.
if ((-d $dne) and (not (File::Spec->file_name_is_absolute ($dne))))
{ $CurDir = `cd`; # Get current Win32 directory
chomp ($CurDir);
@DirPath1 = split (/\\/, $CurDir);
@DirPath2 = split (/\\/, $dne);
$ix = $#DirPath2;
while ($ix-- > -1)
{ $dn = shift (@DirPath2);
if ($dn =~ /^[^A-Za-z0-9_]/)
{ if ($dn eq '..') {pop (@DirPath1);}
elsif ($dn eq '.') {next;}
else
{ $dn = '';
last;
}
}
else {push (@DirPath1, $dn);}
}
$dn = join ("\\", @DirPath1);
}
else {$dn = $dne;}

if (not (-d $dn))
{ print "Invalid directory entered: " . $dne;
}
else
{ print "\n\nCurrent Directory: " . $CurDir;
print "\nNew directory: " . $dn;
}
exit;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives