Solved

Finding the current path.

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 106
Sending email via Perl on Windows 3 208
Perl script to parse log and delete the file 17 183
Strange perl issue 6 136
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…

739 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