how to process each directory

Discussion in 'Perl Misc' started by monk, Feb 27, 2008.

  1. monk

    monk Guest

    Hi all, I was wondering if you guys can help me figure this out.

    This is what I'm trying to do:

    read in a file containing paths to directories /home/foo/logs,
    etc...all fine here.
    I'd like to get into those directories and process the files inside.
    Problem in this part.

    This is what I have:

    ##slurp file into @directories_to_clean blah blah blah...

    foreach (@directories_to_clean){
    chomp;
    print "$_\n"; #testing if I got the right directories and I do
    ex: /home/foo/logs


    #Here is the problem. It stops below claiming there's no file or
    directory.
    #same thing if I try to set a variable instead of default $_

    # Isn't $_ equivalent to /home/foo/logs above ???

    opendir (ARCHIVE, $_) or die "what the?...$!";
     
    monk, Feb 27, 2008
    #1
    1. Advertising

  2. On Feb 27, 3:37 pm, monk <> wrote:
    > Hi all,  I was wondering if you guys can help me figure this out.
    >
    > This is what I'm trying to do:
    >
    > read in a file containing paths to directories  /home/foo/logs,
    > etc...all fine here.
    > I'd like to get into those directories and process the files inside.
    > Problem in this part.
    >
    > This is what I have:
    >
    > ##slurp file into @directories_to_clean blah blah blah...
    >
    > foreach (@directories_to_clean){
    >     chomp;
    >     print "$_\n";    #testing if I got the right directories and Ido
    > ex: /home/foo/logs
    >
    >      #Here is the problem.  It stops below claiming there's no file or
    > directory.
    >      #same thing if I try to set a variable instead of default $_
    >
    >       #   Isn't    $_   equivalent to /home/foo/logs above    ???
    >
    >     opendir (ARCHIVE, $_) or die "what the?...$!";


    Can you post a workable piece of code, along with the actual message
    returned by Perl, along with perhaps a copy/paste of you "ls'ing" to
    one of those dirs successfully?
     
    nolo contendere, Feb 27, 2008
    #2
    1. Advertising

  3. monk

    monk Guest

    > Can you post a workable piece of code, along with the actual message
    > returned by Perl, along with perhaps a copy/paste of you "ls'ing" to
    > one of those dirs successfully?


    Here you go:

    # read config file in
    # add inside test.conf directories such as /home/foo/logs or whatever.
    save the file.
    open($log_file_handler,"<", "test.conf") or die "Can't open config file
    \n";


    DISTRIBUTION_CENTER:
    while (<$log_file_handler>) {
    chomp;
    s/#.*//; # Remove comments
    s/^ *//; #Remove leading spaces
    s/ *$//; #Remove trailing spaces
    next DISTRIBUTION_CENTER if /^(\s)*$/; # skip blank lines

    push @directories_to_clean, $_; #add entries to an array

    }


    #it's supposed to go inside each directory
    foreach (@directories_to_clean){
    chomp;
    print "$_\n"; #testing if I got the right directories and I do
    ex: /home/foo/logs
    opendir (ARCHIVE, $_) or die "what the?...$!";
    chdir $_; #it doesn't get this far

    }

    ERROR: what the hell?...No such file or directory.
     
    monk, Feb 27, 2008
    #3
  4. On Feb 27, 3:48 pm, monk <> wrote:
    > > Can you post a workable piece of code, along with the actual message
    > > returned by Perl, along with perhaps a copy/paste of you "ls'ing" to
    > > one of those dirs successfully?

    >
    > Here you go:
    >
    > # read config file in
    > # add inside test.conf directories such as /home/foo/logs or whatever.
    > save the file.
    > open($log_file_handler,"<", "test.conf") or die "Can't open config file
    > \n";
    >
    > DISTRIBUTION_CENTER:
    > while (<$log_file_handler>) {
    >         chomp;
    >         s/#.*//; # Remove comments
    >         s/^ *//; #Remove leading spaces
    >         s/ *$//; #Remove trailing spaces
    >         next DISTRIBUTION_CENTER if /^(\s)*$/;  # skip blank lines
    >
    >         push @directories_to_clean, $_; #add entries to an array
    >
    > }
    >
    > #it's supposed to go inside each directory
    > foreach (@directories_to_clean){
    >     chomp;
    >     print "$_\n";    #testing if I got the right directories and Ido
    > ex: /home/foo/logs


    Here, try:
    if ( -d $_ ) {
    print "this is a directory\n";
    else {
    print ">>$_<< is not a directory.\n";
    }

    >     opendir (ARCHIVE, $_) or die "what the?...$!";
    >     chdir $_;  #it doesn't get this far
    >
    > }
    >
    > ERROR: what the hell?...No such file or directory.
     
    nolo contendere, Feb 27, 2008
    #4
  5. monk

    monk Guest

    I get from the shell:

    << is not a directory.r/logs
    what the hell?...No such file or directory

    Then I do a manual regular cd command to the directory logs and I'm
    in.
    Even with the full path which the program itself printed with print "$_
    \n";

    any clues?
     
    monk, Feb 27, 2008
    #5
  6. monk

    monk Guest

    Thanks for the tip 'od -c <filename>'

    I uncovered an extra character was being added somehow when slurping
    the config file.
    I added one little line to my code and now it works like a charm.

    foreach (@directories_to_clean){
    chomp;

    chop; # one extra line that solved my problem.

    print "$_\n";

    #and everything else is the same

    }

    Thanks everybody again.
     
    monk, Feb 27, 2008
    #6
  7. Tad J McClellan, Feb 28, 2008
    #7
  8. monk

    monk Guest

    On Feb 27, 1:51 pm, Jim Gibson <> wrote:
    > In article
    > <>,
    >
    >
    >
    > monk <> wrote:
    > > Thanks for the tip 'od -c <filename>'

    >
    > > I uncovered an extra character was being added somehow when slurping
    > > the config file.
    > > I added one little line to my code and now it works like a charm.

    >
    > > foreach (@directories_to_clean){
    > > chomp;

    >
    > > chop; # one extra line that solved my problem.

    >
    > > print "$_\n";

    >
    > > #and everything else is the same

    >
    > > }

    >
    > That is not a good, long-term solution. It is not likely that slurping
    > the file is adding extra characters. It is more likely that the file
    > does not have the proper line endings for your system. As soon as
    > somebody edits your file with the proper editor, it may remove the
    > extra characters and your program will no longer work.
    >
    > You should determine exactly what the extra characters are and remove
    > them. chop will remove the last character of your string, regardless of
    > what it is.
    >
    > In your original program, which you do not show, you have already used
    > chomp on the lines read from your file, and the chomp shown above is a
    > no-op. chomp will remove the expected line-ending character or
    > characters from a string. If they are not found, chomp does nothing.
    >
    > Use the substitute operator or the tr operator to remove only those
    > characters that do not belong:
    >
    > s/[\r\n]+//;
    > tr/\r\n/d;
    >
    > --
    > Jim Gibson


    Jim, you're absolutely right.
    So yeah, using <od -c filename> I identified the extra characters that
    were added and then used regular substitution. This is a much more
    efficient approach. Now I'm just removing only the ones I need. Not
    just whatever was there.

    thanks,
     
    monk, Mar 4, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    412
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    430
    Tjerk Wolterink
    Nov 3, 2004
  3. Pat Maddox
    Replies:
    6
    Views:
    158
    Marcin Mielżyński
    Jan 20, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    142
    PerlFAQ Server
    Jan 26, 2011
  5. PerlFAQ Server

    FAQ 6.14 How do I process each word on each line?

    PerlFAQ Server, Apr 8, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    159
    PerlFAQ Server
    Apr 8, 2011
Loading...

Share This Page