Solved

Finding the current path.

Posted on 2002-05-22
8
208 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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