Solved

Finding the current path.

Posted on 2002-05-22
8
188 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
Comment Utility
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
Comment Utility
Does not work.
It returns the path of the executing script (A)!
0
 
LVL 1

Expert Comment

by:GorGor1
Comment Utility
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
 

Author Comment

by:yafitmayo
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Accepted Solution

by:
ozo earned 70 total points
Comment Utility
print __FILE__;
0
 
LVL 1

Expert Comment

by:sushrut
Comment Utility
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
Comment Utility
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
Comment Utility
if you want a pure perl code,
use following to get directory name (tested on AIX)

use Cwd ;
print getcwd ;

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now