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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to manipulate chdir()

Status
Not open for further replies.

sappleg

Programmer
Apr 15, 2008
31
CA
Hi all,

I am writing a subrouting in perl where I am using chdir to change the directory to a mapped drive/directory. If it successfully changes the drive, then it proceeds further. However, if the chdir fails (it means the drive is not mapped) then the script maps the drive (with net use).

Oh! By the way, I don't want to use 'Die'. Please let me know how to evaluate if the last statement is successful.

This is driving me crazy. Any help would be really appreciated.

Here's my sample code:

sub map_check();
{
chdir($map_drive);
if chdir is successful (means the drive is mapped)
{
<do something>
}
else
{
`net use s: \\\\server\shared`;
copy_files();
}
}
 
chdir's own return value will tell you if it succeeded or not. i.e.:
Code:
if ( chdir( $map_drive ) ) {
   # it worked
}
else {
   # it didn't
}
 
And on a slightly less relevant note..., you shouldn't use the backticks if you're not collecting the output.

Or so it says in the Perl Cookbook (Chapter 16, section on "Gathering Output from a Program")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top