Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Perl module(s) do I use to obtain an absolute path (including filename) from a relative one on Windows?

I can only imagine I'm not searching correctly; this seems like an obvious question to be asked here. My apologies if this is a duplicate.

I'm writing a Perl program that will take a filename as a command-line argument. I need to convert the filename (or the filename with a relative path attached) to an absolute path (specifically to work with Win32::OLE).

I tried using Cwd's 'abs_path', and that almost does what I want, but it returns it using a Unix-style path instead of a Win32 one.

Is there a module that will convert the path, or perhaps a better module to use in the first place?

like image 525
romandas Avatar asked Oct 06 '09 19:10

romandas


People also ask

How do I find the absolute path of a file in Perl?

use File::Spec; ... my $rel_path = 'myfile. txt'; my $abs_path = File::Spec->rel2abs( $rel_path ) ; Save this answer.

How do you convert relative path to absolute path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

How do you find the absolute path?

To view the full path of an individual file: Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document.

How do you use absolute and relative paths?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

Which of these is an absolute file path?

An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.

What is absolute path command?

What is an absolute path? An absolute path is the full path to a file or directory. It is relative to the root directory ( / ). Note that it is a best practice to use absolute paths when you use file paths inside of scripts. For example, the absolute path to the ls command is: /usr/bin/ls .


2 Answers

I use rel2abs from File::Spec. You have to be careful though: that might call getdcwd from Cwd, and it will assume that you want the current working directory for the current drive. If the file is on some other drive, you'll have to fix that up yourself or supply the second argument to set the base path.

like image 70
brian d foy Avatar answered Nov 15 '22 06:11

brian d foy


use File::Spec::Functions qw(rel2abs);
print rel2abs($ARGV[0]), "\n";
like image 33
FMc Avatar answered Nov 15 '22 06:11

FMc