• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

find scripts directory location


If $0 gives the location of the script you are running, how can I just get the directory where the script is located?

CWD; doesn't work the I want, since if I execute a script located on a network drive from the commandline on my local PC, the working directory would be whatever directory I'm in at my shell.


0
Marketing_Insists
Asked:
Marketing_Insists
  • 2
1 Solution
 
GnarOlakCommented:
If what you are asking is how to extract from $0, which has both a path and the name of the script, just the directory then you can use File::Basename thus:

use File::Basename;

$script = basename ($0);
$dir = dirname($0);

print "$script was run from $dir\n";
__END__

If that isn't what you need then please elaborate.

0
 
Marketing_InsistsAuthor Commented:
Yes, that's it exactly, thanks!
0
 
tone28Commented:
I would probably

my @parts = split('\\\\',$0);

pop(@parts);

print join "\\",@parts;
0
 
tone28Commented:
oops too late.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now