Solved

Finding the current path.

Posted on 2002-05-22
8
199 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Uploading Binary Data using Perl 5 83
hard perl script 16 154
pattern matching in perl 2 101
perl split by | 2 90
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

920 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

16 Experts available now in Live!

Get 1:1 Help Now