Solved

Finding the current path.

Posted on 2002-05-22
8
226 Views
Last Modified: 2010-07-27
I have script A that calls script B by using the "do" method. In script B, I need to know what is the current path of the current script (B).
When I use getCWD(), I get the path of the executing script!!! Which I don't want...
Does anyone know how can I do this?
0
Comment
Question by:yafitmayo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:sushrut
ID: 7026800
If you dont need pure perl version

Unix
$present_working_Directory=`pwd`;
print "$present_working_Directory \n";

Windows
$present_working_Directory=`cd`;
print "$present_working_Directory \n";
0
 

Author Comment

by:yafitmayo
ID: 7026816
Does not work.
It returns the path of the executing script (A)!
0
 
LVL 1

Expert Comment

by:GorGor1
ID: 7028037
I don't understand your question.  Why not just execute the getCWD in script B?  That will tell you the CWD of script B.  I guess I'm confused.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:yafitmayo
ID: 7029025
I'll explain:
script_A is under c:\perl\
script_B is under c:\perl\test\

code:
script_A:
do ".\test\script_B.pl";

script_B:
use CWD;
print getCWD();

The output I get, when I run script_A is: c:\perl\
Instead of c:\perl\test

I would like script_B to print its current directory and not the executing directory.
0
 
LVL 84

Accepted Solution

by:
ozo earned 70 total points
ID: 7029086
print __FILE__;
0
 
LVL 1

Expert Comment

by:sushrut
ID: 7034988
Well.. I think your CWD is c:\perl and not c:\perl\test

If you want b.pl to write in c:\perl\test you may need to change directory to c:\perl\test
0
 
LVL 1

Expert Comment

by:sushrut
ID: 7034997
Use this in b.pl
This code works on Win32

print "before ";

system ("cd");

$path_name =  __FILE__;

if ($path_name  =~ /(.*)\\.*/)
{
$path_dir = $1;
}

use Cwd 'chdir';
chdir $path_dir;

print "after ";
system ("cd");



To make it work on Unix (Tested on AIX)

print "before ";
system("pwd");
$path_name =  __FILE__;
if ($path_name  =~ /(.*)\/.*/)
{
$path_dir = $1;
}

use Cwd 'chdir';
chdir $path_dir;

print "after ";
system("pwd");

0
 
LVL 1

Expert Comment

by:sushrut
ID: 7034999
if you want a pure perl code,
use following to get directory name (tested on AIX)

use Cwd ;
print getcwd ;

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question