My OS is WINNT4. Any pointers would be greatly appreciated.
Rafal
--
=================
Rafal S. Konopka
<raf...@home.com>
=================
use File::Find;
>I tried using the example in the Camel book on p.
>56 but it only reports the first two levels and only on the first
>subfolder it encounters.
Huh? There is no code at all on p56 of the Camel book. There is
some code on p56 in the older second edition of the Camel, but
it does not do directory searching. I do not have a 1st edition
ready to hand. What edition of the Camel are you referring to?
Why not just show us the code that you used?
(then throw it away and use File::Find instead :-)
--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas
> I need a listing of all the *.htm files in an given directory and all
> its subdirectories. I tried using the example in the Camel book on p.
> 56 but it only reports the first two levels and only on the first
> subfolder it encounters.
This script will do what you want. I use unix, so I made the assumption
(which could be wrong) that the correct way to refer to a path is with
"\\", not "/" (eg, "./usr/bin/perl" in unix is written in perl as
".\\usr\\bin\\perl". If I'm wrong you need to replace my \\ with the
proper delimiter before this script would work.
$|=1;
$dir = ".";
&recursive_directory_traversal($dir);
print "\n";
sub recursive_directory_traversal {
my $parent = shift;
my (@children, @trim, $offset);
$offset = 0;
opendir(PARENT, $parent) or die "Can't open $parent\nReason:$!\n\n";
@children = grep(!/^[\.]{1,2}$/, readdir PARENT);
foreach my $index (0..$#children){
if (-d "$parent\\".$children[$index]){
recursive_directory_traversal("$parent\\".$children[$index]);
push(@trim, $index);
}
}
foreach my $index (@trim){
splice(@children, $index - $offset++, 1);
}
print "$parent :\n" . join("\n", @children) . "\n";
}
> My OS is WINNT4. Any pointers would be greatly appreciated.
That's a shame because if you were using unix you'd just:
find /path/to/html/files/top/directory -name \*.htm -print
sysop> This script will do what you want.
No, this script also follows symlinks erroneously.
DO NOT REINVENT DIRECTORY RECURSION (badly).
use File::Find
use File::Find
Please.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Sure, File::Find gives you all kinds of protection, allowing you to
follow symlinks without getting trapped in recursion. But I thought
maybe just a little snippet would do. I wasn't trying to reinvent
File::Find, I looked for FQPN at search.cpan.org, and scanned the File
Handle section, before bothering to write my own snippet. Nor was my
code supposed to be as universal as File::Find, it was simply to give
the guy the list of files in his directory. Do you blame me for trying
to help a fella out?
I also didn't know that there WERE symlinks in a Windows machine's
filesystem. (How does one go about creating them?) However, to keep
from following said symlinks you can, rather simply, check with -l,
like so:
...
if ((-d "$parent\\".$children[$index]) && (!(-l
"$parent\\".$children[$index]))){
...
Cheers
>This script will do what you want.
I don't think so. It does not list all the *.htm files.
[ snip 25 lines of code ]
Let's do it with a core module instead:
-----------------------------------
#!/usr/bin/perl -w
use strict;
use File::Find;
my $dir = '/home/httpd';
print "$_\n" foreach find_htm($dir);
sub find_htm {
my @htm;
find sub { push @htm, $File::Find::name if /\.htm$/ }, @_;
return @htm;
}
-----------------------------------
http://www.geocities.com/nnqweb/nquote.html
Text rearranged into actual chronology.
]
sysop <vze3...@verizon.net> wrote:
>On Mon, 19 Nov 2001 21:14:58 -0500, Randal L. Schwartz wrote:
>
>>>>>>> "sysop" == sysop <vze3...@verizon.net> writes:
>>
>> sysop> This script will do what you want.
>>
>> No, this script also follows symlinks erroneously.
>>
>> DO NOT REINVENT DIRECTORY RECURSION (badly).
>>
>> use File::Find
>>
>> use File::Find
>>
>> Please.
>>
>Hmmm, me thinks you've had too much caffine recently...
Methinks you're applying for a negative scorefile entry.
Perhaps Randal has a reason that you just do not know of. Do
you know who Randal is? If you think he said something wrong,
it is best to reconsider five to ten times before you call
him on it, else you could end up embarrassing yourself.
>Perhaps I
>should start any replies I post to this newsgroup with:
Perhaps you should lurk in the newsgroup for a while before
you start posting at all. This is normal procedure for
Usenet newsgroups.
How can you know what is socially acceptable if you do not
observe for a while?
>I am not the Oracle, nor should communication with me be held to be as
>undeniably true as communication with god.
You have actually pretty much hit on the exact reason right there.
Without such a disclaimer folks will copy code here and use it.
Then they will be back to deal with the problems in the code.
Time taken helping them fix it is time not spent helping other
Perl programmers. It is stealing time from our peers.
Better to just give them a good answer the first time.
If you are not reasonably certain that it is a good answer,
then don't post it.
>Do you blame me for trying
>to help a fella out?
Showing a 10 line File::Find solution would have helped him
out even more, and be easier to understand as well.
>if ((-d "$parent\\".$children[$index]) && (!(-l
^^
And you can use forward slashes for the directory separator
on Windows just fine, as long as you don't feed it to the
command interpreter.
--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas
Posting Guidelines: http://mail.augustmail.com/~tadmc/clpmisc.shtml
: I need a listing of all the *.htm files in an given directory and all
: its subdirectories. I tried using the example in the Camel book on p.
: 56 but it only reports the first two levels and only on the first
: subfolder it encounters.
:
: My OS is WINNT4. Any pointers would be greatly appreciated.
use File::Find;
perldoc -f glob
-- fxn
>Methinks you're applying for a negative scorefile entry.
>
>Perhaps Randal has a reason that you just do not know of. Do
>you know who Randal is?
Hmm... I think it's time for a new slogan, in the realm of "stop feeding
the trolls". It is: "stop slamming the newbies". Come out of your ivory
tower. It's time to think on how, what you (anybody) have written in one
post, looks like to ordinary people who have never seen anything by you
before.
Although Randal looks like a very nice guy to me, judging from his
website (I really like people who dare to appear silly) (for those who
don't know where it is, it's at <http://www.stonehenge.com/merlyn/>), a
situation pretty much like this has brought him in serious legal
trouble. OK, IANAL, but at least, it looks to me that way: to those who
have been (remotely) in touch with him, it seems odd that his
"arrogance" was what appears to have been the prime reason to prosecute
him. See the closing statement of the trial at
<http://www.lightlink.com/spacenka/fors/court/pclose.txt>:
"That arrogance is what brings him here today."
(ok I'll stop now)
Well, apparently, Randal's recent post appears equally "arrogant" to
this person. How can he know that Randal has been hammering this point
here, at least 20 times, last year alone. (estimate)
--
Bart.
Thanks, Bart. That really needed to be said. The problem with arrogant
people is that they can't accept the fact that they have a problem; and
realization and acceptance is always a mandated prerequisite to any
hopeful cure.
Regards,
--Dennis
---------------------------------------
"I don't understand... whas yo plan?... that you can't be... good to me.
Be Good to ME!" --Tina Turner
>Tad's solution was
>informative and looked good and elegant. Unfortunately, it exited with
>an error.
I expect you meant error _message_?
Care to share the error message text with us?
It gave me no errors (ie. functioned correctly) and also
no error messages, so I'm wondering what was wrong with
it. Don't leave me guessing.
>Likely, the version of perl (5.003) that is running on my
>machine is to blame.
Yikes!
5.004 came out in 1997. That's a Long Time in computer years.
Even worse, there is a CERT advisory against 5.003.
Get with the times man! :-)
Many thanks to all who replied. Wow, talk about a storm in a teacup!
If I may offer my two cents, I think it's good to realize that pople who
post questions to this group--and I have been a lurker and help-seeker
for many years now (though the frequency is probably 2/year) are not all
Perl pros. The majority come here with a legitimate problem, and often
(I certainly consider myself in this category) seek some learning
experience. To many of us, perl is a great tool, where with a couple of
simplelines of code you can do what would take hours to do by hand. And
sometimes a quick and dirty solution is all we really need.
I got a good chuckle from sysop's comment on Unix and find--in my
previous incarnation, I was a Unix sysadmin. In fact, I could have very
easily gone for a solution where system("dir *.htm /b/s") would get
loaded to a list array and that would be it (even simpler than "find"
:-). I wanted to learn, however, how to do it in "pure" perl.
Yes, Randal, I did look up file::find, but couldn't make heads or tails
of the manpage. That was not very helpful :-(. Tad's solution was
informative and looked good and elegant. Unfortunately, it exited with
an error. Likely, the version of perl (5.003) that is running on my
machine is to blame. And yes, Tad, having spent years in academia, I
should have known better: I should have mentioned that the Camel book I
was using was the very first edition (1990).
Indeed, sysop's solution was the one I adopted. It too produced errors
(my perl didn't like "my" operator, but I could modify the code very
quickly to do exactly what I wanted. It was a good "learning
experience."
Once again, many thanks to all; Happy Thanksgiving...and please more
patience with semi-newbies :-).
Tad McClellan wrote:
>
> I expect you meant error _message_?
>
> Care to share the error message text with us?
>
Tad,
Here's your code (I modified the $dir variable):
#!/usr/bin/perl -w
use strict;
use File::Find;
my $dir = '.';
print "$_\n" foreach find_htm($dir);
sub find_htm {
my @htm;
find sub { push @htm, $File::Find::name if /\.htm$/ }, @_;
return @htm;
}
The error message it produces is:
Missing $ on loop variable at test1.pl line 6.
which is the "...foreach find_htm($dir);" line
I have not had an opportunity to test your code on my work PC, where I
have installed Active Perl 5_6_?. It seems like my (work) alter ego did
get with the times more than my home one :-).
When I get back to my office (10 days or so), I'll test it and let you
know.
BTW, how would you amend the above code to allow for filtering some
parent-level directories from search--i.e. out of say six folders in "."
I only really want to search through 2 or 3, disregarding the rest.
Regards,
I suspect that you have a bug in your version of Perl. I cannot
reproduce your reported error with perl 5.6.1 (Solaris).
--
Garry Williams
>print "$_\n" foreach find_htm($dir);
>The error message it produces is:
>
> Missing $ on loop variable at test1.pl line 6.
It is your Perl-of-only-historical-interest that is biting you.
Upgrade!
Perl is free. It installs in minutes.
Or, write the foreach loop the old-fashioned way with lots of
punctuation to get in the way of reading and understanding it:
foreach ( find_htm($dir) ) {
print "$_\n";
}
>I have not had an opportunity to test your code on my work PC, where I
>have installed Active Perl 5_6_?. It seems like my (work) alter ego did
>get with the times more than my home one :-).
>
>When I get back to my office (10 days or so),
You seem to be overly patient :-)
You won't have 15 free minutes to spend in the next week and a half?
>I'll test it and let you
>know.
Please clearly state what version of Perl you are using.
I want to skip your posts if it is not 5.005_03 or better.
I don't want to have to keep remembering how you used to have
to do things.
>BTW, how would you amend the above code to allow for filtering some
>parent-level directories from search--i.e. out of say six folders in "."
>I only really want to search through 2 or 3, disregarding the rest.
Take them out of the @_ array before you call find() if you
don't want them to be searched.
Tad McClellan wrote:
> Upgrade!
>
I will, but it's not that simple. To give you an example. On my office
machine, I have 5.6.?. Being an SGML consultant, you're probably
familiar with Chrystal Astoria DMS. I requested from them the Perl API
to Astoria and guess what: according to their guys, the Perl API has
been tested and works with 5_2_2. On 5_6_ it crashed (not just an error
message :-)). Should I then "downgrade" to 5_2_2?
Which Perl version would you recommend?
> Or, write the foreach loop the old-fashioned way with lots of
> punctuation to get in the way of reading and understanding it:
>
> foreach ( find_htm($dir) ) {
> print "$_\n";
> }
>
I tried that too and this time nothing happened, except for a warning:
Name "File::Basename::VERSION" used only once: possible typo at
C:\Perl\lib/File/Basename.pm line 121.
But indeed there's no point in testing that grandpa-perl. I'll upgrade
and then see what happens.
> >When I get back to my office (10 days or so),
>
> You seem to be overly patient :-)
>
> You won't have 15 free minutes to spend in the next week and a half?
No, I won't be anywhere near my office until the week of December 3.
BTW, are you by any chance going to XML-World?
> Please clearly state what version of Perl you are using.
>
> I want to skip your posts if it is not 5.005_03 or better.
>
On my laptop and my olde PC at home, I'm using 5.003_07. I'll certainly
upgrade asap...certainly before I bother the group again.
Roger and out (I'll be incomunicado foe all of next week)
Rafal
--
...Stand beside her and guide her
through the night with the light from above.