qwerty80:
You already have several good answers, so I'm not really adding much. However, I've not played much with opendir or readdir and I thought I'd like to play with them a bit. I didn't want a recursive solution, so I did it this way:
$ cat 1004138.pl
#!/usr/bin/perl
use strict;
use warnings;
use 5.10.0;
use autodie;
my $filespec = "abc*.xml";
my @dirs = ('ABC');
while (@dirs) {
my $curdir = pop @dirs;
opendir(my $dh, $curdir);
for my $f (readdir($dh)) {
next if $f =~ /^\.\.?$/;
next unless -d "$curdir/$f";
print "pushing $curdir/$f\n";
push @dirs, "$curdir/$f";
}
for (glob("$curdir/$filespec")) {
print "processing $_\n";
}
}
$ perl 1004138.pl
pushing ABC/DEF
pushing ABC/GHI
pushing ABC/JKL
processing ABC/abcde.xml
processing ABC/JKL/abc_x.xml
processing ABC/JKL/abc_y.xml
processing ABC/JKL/abc_z.xml
pushing ABC/GHI/baz
pushing ABC/GHI/foo
pushing ABC/GHI/bar
processing ABC/GHI/abc_5.xml
processing ABC/GHI/abc_6.xml
processing ABC/GHI/bar/abc_1.xml
processing ABC/GHI/bar/abc_2.xml
processing ABC/GHI/bar/abc_3.xml
processing ABC/GHI/bar/abc_4.xml
processing ABC/GHI/foo/abc_8.xml
processing ABC/GHI/foo/abc_9.xml
processing ABC/GHI/foo/abc_a.xml
processing ABC/GHI/foo/abc_b.xml
processing ABC/GHI/baz/abc_{7}.xml
It's not particularly pretty, but it amused me for a few minutes. I didn't actually let it do file deletions because I'm loath to do automatic deletes until I've well tested such a script. It's a good thing, too. On my earlier attempt, a pair of bugs conspired to make the script "process" all "abc*.xml" files on my hard drive, rather than just those in the ABC directory.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|