Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding directory of executing script 1

Status
Not open for further replies.

Aneusomy

Programmer
Jan 15, 2001
20
0
0
CA
Hi all,

Does anyone know how to find the directory path of the script that's being run? This is assuming that the script is being called somewhere other than where it is located, i.e. the script is in the path. Something similar to 'whereis' in *nix (I'd prefer not to make any system calls-- I want this to be portable to W32).

eg:
script location: /users/aneusomy/test.pl
current directory: /
I want when I run test.pl from / , the program to report:
/users/aneusomy/

I thought this may be in the %ENV but no luck, and I can't find any built-in perl functions that do this. The cwd() function only reports, as expected, the current working directory.
Any help is appreciated! (heh - this is not an April Fool's joke :) )
Ben




 
use Cwd;
$dir = cwd;
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi Mike,
Thanks for the response, but I think you read my post in a hurry. cwd only gives where you're running from, not the directory where the script actually resides.
Maybe what I'll try is parsing the path and finding if the script is located in any of the directories in the path. Otherwise, I think there may not be a quick way to do this.
Ben
 
oh, sorry

use File::Basename;
my $path_to_script = dirname($0);
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top