?
Solved

chdir in perl script not working well

Posted on 2003-02-19
4
Medium Priority
?
1,417 Views
Last Modified: 2008-03-10
the script hold list of directories :

#!/usr/local/bin/perltk -w
 ...


foreach $dir (@all_dirs)  {

$pwd = `pwd`;
printf "curr dir : $pwd\n";
chomp $pwd;

printf "Open dir: $pwd/$dir \n";
chdir("$pwd/$dir");

$pwd2 =  `pwd`;
printf "curr dir now : $pwd2\n";



    $ipg_file = 'dev00_gmii_ipg_data.fws';
    if (-e 'dev00_gmii_ipg_data.fws') {
        $ipg_file = 'dev00_gmii_ipg_data.fws';
    } elsif (-e 'dev00_ipg_data.fws') {
        $ipg_file = 'dev00_ipg_data.fws';
    } else {
        print "Files dev00_gmii_ipg_data.fws and dev00_ipg_data.fws don't exists!!!!\n\n\n";
        exit;
    }
..
}

but as you can see , I added print for locations and lothing changed , beside , it is NOT changing dirctory
0
Comment
Question by:Talmash
[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
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 100 total points
ID: 7982240
Please change this line:

chdir("$pwd/$dir");

to

print "chdir to $pwd/$dir\n";
chdir("$pwd/$dir") or warn( "chdir failed: $!");


and show us the output. As it is, we don't have enough information to go on.

Most likely, the solution to the problem is to have the directories in your list @all_dirs be absolute path names rather than relative path nmaes so you don't have to keep track of your initial directory after you start doing chdir.
0
 
LVL 1

Assisted Solution

by:mikezone
mikezone earned 100 total points
ID: 7982513
I don't know if the exit() you have in your loop is intentional, and I'm guessing it's supposed to be outside of the loop. Otherwise, the program will probably exit after searching the first path.

A wonderful module that a co-worker introduced to me was Cwd, which has such commands as getcwd() and abs_path(), which gets the current working directory, and converts relative paths to absolute paths, respectively. Best of all, it's portable (unlike `pwd`) and it's part of the standard Perl distribution.

You could:

  use Cwd qw( getcwd abs_path );

  $pwd = getcwd();
  $dir = abs_path( $dir );
  chdir( $dir ) or die "Can't change paths to '$dir'";

to fix the potential relative path problem in a portable fashion.

But it looks like what you really want is a way to find one of two .fws data file, and for that I suggest the File::Find module, also part of the standard Perl distribution. For that, I suggest:

  use Cwd;
  use File::Find;

  $pwd = getcwd();
  my $ipg_file;

  find( \&wanted, $pwd );

  sub wanted {
    if( /^dev00_(gmii_)?ipg_data.fws$/ ) {
      $ipg_file = $_;
    }
  }

There's a lot of magic in File::Find, and it's a great module to learn to use. For more information on either module, check the Camel book, perldoc File::Find, and perldoc Cwd.

hth,

- m.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9691730
Nothing has happened on this question in over 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between jmcg [25 pts] and mikezone [25 pts].

Please post any comments here within the next seven days. Moderators check comments here before acting on the recommendation. Experts: silence will likely be taken as assent.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

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!

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
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…
Six Sigma Control Plans
Suggested Courses

765 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