Solved

How do you get the current working directory ( CWD ) on Windows, where the script lives, for example

Posted on 2004-09-29
10
5,057 Views
Last Modified: 2008-01-09
I need to execute a program that is parallel to my script, but need full path to cwd.  I know how to execute it by hardcoding the cwd, but how do I get it at runtime?
0
Comment
Question by:mark_at_acc
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12184317
$path = system('pwd');

cheers
0
 

Author Comment

by:mark_at_acc
ID: 12184405
The problem is, some machines that the script will run on will have cygwin tools installed...in that case, you end up with a path like:

         /cygdrive/c/marks/bii4

instead of:

         c:\marks\bii4

Sorry, should have mentioned that initially...
0
 
LVL 8

Accepted Solution

by:
inq123 earned 200 total points
ID: 12184409
Hi mark_at_acc,

this one would work on windows:

use File::Spec;

my $cwd = File::Spec->rel2abs(".");

Cheers!
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 12184421
use Cwd;
my $dir = getcwd;
0
 
LVL 8

Expert Comment

by:inq123
ID: 12184484
FishMonger,

Nice solution!  But getcwd returns incorrect path separator on Windows (at least in case for ActivePerl, but I imagine the same case for perl on cygwin too, which OP pointed out as a problem).  Just a thought.
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 28

Expert Comment

by:FishMonger
ID: 12184548
inq123 ,

You're right.  I didn't refresh my screen prior to posting so I missed Mark's updated post, and since I'm used to having Perl handle the conversion of the path separator (except when it's a system call), I forgot about that caviot.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 12184561
inq123 ,

BTW, it's nice to see ya back.  I missed our friendly bouts.  :)
0
 
LVL 15

Assisted Solution

by:nicholassolutions
nicholassolutions earned 50 total points
ID: 12184724
>>/cygdrive/c/marks/bii4

I'm not too familiar with cygwin (I keep meaning to download it but havent had time).

Could you possibly use regular expressions to strip out the 'wrong' path info? You should be able to if the possible errors are predictable (I'm not sure if 'cygdrive' is literally what is returned, or if that is just a variable you are using as an example). For example:

$path = system('pwd');
$path =~ s/cygdrive\/([a-z])/$1:/i;

Cheers,
Matt
0
 

Author Comment

by:mark_at_acc
ID: 12185038
I think I found the solution...even with cygwin installed, running the DOS "cd" command by itself with no params returns $cwd nicely, with drive volume and backslashes...

I thought of doing the parse Matt, but thought there must be a better way.  First I would need to check if the path looked wrong, then change it, otherwise keep the original...I had to believe Windows could give up CWD pretty easily, even with cygwin installed.

Will assign points shortly, Thanks all!!
0
 
LVL 8

Expert Comment

by:inq123
ID: 12186789
Hi, Fishmonger, nice of you to say that!  I'm glad to be back too.  I've been too busy for a while but glad to be less busy recently to pick up where I left last year.

I see that you're doing quite well in all areas especially perl.  Great job!  See you around.
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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

21 Experts available now in Live!

Get 1:1 Help Now