Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to process each directory

Reply
Thread Tools

how to process each directory

 
 
monk
Guest
Posts: n/a
 
      02-27-2008
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?...$!";
 
Reply With Quote
 
 
 
 
nolo contendere
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 3:37*pm, monk <(E-Mail Removed)> 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?

 
Reply With Quote
 
 
 
 
monk
Guest
Posts: n/a
 
      02-27-2008
> 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.
 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 3:48*pm, monk <(E-Mail Removed)> 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.


 
Reply With Quote
 
monk
Guest
Posts: n/a
 
      02-27-2008
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?
 
Reply With Quote
 
monk
Guest
Posts: n/a
 
      02-27-2008
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.


 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      02-28-2008
Jim Gibson <(E-Mail Removed)> wrote:

><http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html>
> (currently not accessible to me) and posted here twice a week.



It changed location last summer (at revision 1.:

http://www.rehabitation.com/clpmisc.shtml
or
http://www.rehabitation.com/clpmisc/...uidelines.html


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
monk
Guest
Posts: n/a
 
      03-04-2008
On Feb 27, 1:51 pm, Jim Gibson <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
>
>
>
> monk <(E-Mail Removed)> 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,



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 6.14 How do I process each word on each line? PerlFAQ Server Perl Misc 0 04-08-2011 04:00 AM
FAQ 6.14 How do I process each word on each line? PerlFAQ Server Perl Misc 0 01-26-2011 11:00 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM



Advertisments