Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Retrieve Current folder or folder in which script runs ??? 2

Status
Not open for further replies.

perdu

Programmer
Joined
Apr 17, 2002
Messages
3
Location
BE
Hello,

I've got a perl script installing some software. it has to change folder various time and at end, get back at the one in which we launched the script.
we don't know which folder it is because it is launched by a deployment tools and may be on different drives on different machines.

How can this be done ??? it's on Windows environment (nt4).

I'ven't find any solution yet except launching "exec cd >file.txt" and assign it to $path
i the try to get the value of it but... it's like... s:\temp.

but whe i make chdir($path) of course, it doesn't work...as it consider \t as a tab...

Hope someone can help a newbie
 
Found this little snippet - try and see if it works for you too!

use FindBin qw($Bin); # the capital B is required
print $Bin . "\n";

This little script prints the full path of this script i.e.
if I put this in /home/mydir and run the script the output is, funnily enough, /home/mydir on my Linux system and it also works on NT as well.

That should help eh?

greadey.
 
The perl variable $0 contains the name of the perl script being executed. I haven't used it, so I don't know if that name includes the path or not. Try printing that variable and see what it contains.

Alternatively, before you start changing directories you can use backtics to execute pwd (unix) or cd (windows or dos) to get the current directory, and save it somewhere so you can return to it later.

I haven't tested this, so I've no idea if it will work, but you could also try $INC{yourProgramName}. The %INC hash contains entries for each filename that has been included via do or require. The key is the filename, and the value is the location. It just might also contain an entry for the currently executing file. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
tsdragon is right use the $0
this will print full path and filename
like this :
D:\Coding\batchperlcpp\others\f.pl someone knowledge ends where
someone else knowledge starts
 
Funnily enough on my NT workstation the $0 variable only prints the program name with no extra path information. I have tried the other methods such as using the @INC array but the trouble is that you can't guarantee a pathname since perl seems to store the current directory merely as ".". Not much help eh?

greadey
 
#!path/to/perl -w
# here a way....but that's to bad about the $0 im on 2kp
# assuming u know on witch drive u are drive
use strict;
use File::Find;
my @found = ();
my $foundresult;
my $filename = "yourfilename"; # no extention see below
my $drive = "x:\\"; # put your drive letter in place of x
my $pauser;
find( sub { push @found, $File::Find::name if /$filename\.pl/ }, $drive );
# again you might have more than one file as this name&ext
foreach $foundresult (@found) {
print "\n\t$foundresult";
&pause;
}
print "\nDone";
&pause;
exit;

sub pause {
print "\nHit Enter to continu";
$pauser = <stdin>;
}
# this is not perfect but i hope i get u in the right way
# wink someone knowledge ends where
someone else knowledge starts
 
Thanks for all your answers.

on my nt4Workstation, onlu the BIN solution works.

but thanks for all, it now works fine
 
Did you try my suggestion about using backtics to execute the cd command to get the current directory, and store it somewhere for use later? That seems to me to be the best solution.
Code:
$CurrDir = `cd`;
I'm assuming, of course, that NT will even let you execute a DOS command like cd. I don't use NT, so I've no idea if it will. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
yes, i've tried.

but i tehn receive a path like c:\something

and it is the \ that then annoys me... so i should then substitute this carater to another etc...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top