<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Instead of plugging my own software, I'll mention File::Find because no
one has yet. It should already be on your system.<br>
<br>
On 02/18/10 14:04, Tom Keller wrote:
<blockquote cite="mid:A4D7D0BA-DA04-4BC3-8613-89A9497CAD70@ohsu.edu"
type="cite">Greetings,
<div>I need to be able to process files within a directory tree. I
know there is a module IO::Dir::Recursive, but I have trouble wrapping
my brain around recursion, so I wanted to give it a try just using
IO::Dir. This seems to do the recursion correctly:</div>
<div><br>
</div>
<div>
<div>#!/usr/bin/perl -w</div>
<div>use strict;</div>
<div>use IO::Dir;</div>
<div><br>
</div>
<div>my $source = $ARGV[0];</div>
<div>my $count;</div>
<div><br>
</div>
<div>process_dir($source);</div>
<div>print "$count files in all.\n";</div>
<div><br>
</div>
<div>sub check {</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>my
($dh,$path) = @_;<span class="Apple-tab-span" style="white-space: pre;">
</span>## dirhandle and path</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>while
(defined($_ = $dh->read)) {</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>next
if m/^\.+/;<span class="Apple-tab-span" style="white-space: pre;"> </span>##
skip dot files</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>if
( -f "$path/$_" ) {</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>my
$file = "$path/$_";</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>process_file($file);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>}
elsif ( -d "$path/$_" ) {</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>my
$dir = "$path/$_";</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>process_dir($dir);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>}
else { print "unknown thing\n"; }</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>undef
$dh;<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>}</div>
<div><br>
</div>
<div>sub process_file {</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>my
$file = shift;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>$count++;</div>
<div>}</div>
<div><br>
</div>
<div>sub process_dir {</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>my
$path = shift;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>my
$dh = IO::Dir->new($path);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>check($dh,$path);</div>
<div>}</div>
<div><br>
</div>
<div>## ... I'm guessing there's a better way, so I'm asking for
feedback. </div>
<div><br>
</div>
<div>thanks,</div>
<div><br>
</div>
</div>
<div><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span
class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="word-wrap: break-word;"><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="word-wrap: break-word;"><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="word-wrap: break-word;"><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="word-wrap: break-word;"><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="word-wrap: break-word;"><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="word-wrap: break-word;"><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;"><span
class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="font-family: Helvetica;"><span class="Apple-style-span"
style="font-family: Helvetica;">Tom</span></div>
<div style="font-family: Helvetica;"><span class="Apple-style-span"
style="font-family: Helvetica;"><a moz-do-not-send="true"
href="mailto:kellert@ohsu.edu">kellert@ohsu.edu</a></span></div>
<div style="font-family: Helvetica;"><span class="Apple-style-span"
style="font-family: Helvetica;">503-494-2442</span></div>
</span></span></div>
</span></div>
</span><br class="Apple-interchange-newline">
</div>
</span><br class="Apple-interchange-newline">
</div>
</span><br class="Apple-interchange-newline">
</div>
</span><br class="Apple-interchange-newline">
</div>
</span><br class="Apple-interchange-newline">
</span><br class="Apple-interchange-newline">
</div>
<br>
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
Pdx-pm-list mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Pdx-pm-list@pm.org">Pdx-pm-list@pm.org</a>
<a class="moz-txt-link-freetext" href="http://mail.pm.org/mailman/listinfo/pdx-pm-list">http://mail.pm.org/mailman/listinfo/pdx-pm-list</a></pre>
</blockquote>
<br>
</body>
</html>