how to process each directory

M

monk

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?...$!";
 
N

nolo contendere

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?
 
M

monk

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.
 
N

nolo contendere

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";
}
 
M

monk

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?
 
M

monk

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.
 
M

monk

monk said:
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, 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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top