not recursing dir's using File::Find

D

david

Im trying to find all files ONLY in the current directory that are
called
"today.txt" or "sub.txt" and copy them to a different directory. Im
trying to do so using File::Find and File::Copy, but I cannot seem to
get this not to recurse through the directory structure. (There may be
other "today.txt" files below the current dir.

#!/usr/bin/perl -w

use strict;
use File::Find;
use File::Copy;

my $dir="./floppy";
my $curdir="./work";

opendir(DIR, $curdir) || die "$!";

sub wanted {
if ( -f $_ && ! -d $_) {
my $file = $File::Find::name;
if ($file =~ /today\.txt/ or /sub\.txt/)
{
for ($File::Find::name)
{
print "Copying $_\n";
copy $file, $dir or die "Error copying: $!";
}
}
}
}
find \&wanted, '.';
close(DIR);


-------------
Current DIR is "/work".
/work/today.txt
/work/sub.txt
/work/folder/today.txt

I only want these files from /work. Any suggestions?

Thanks!
 
J

Jürgen Exner

david said:
Im trying to find all files ONLY in the current directory that are
called
"today.txt" or "sub.txt" and copy them to a different directory. Im
trying to do so using File::Find and File::Copy, but I cannot seem to
get this not to recurse through the directory structure. (There may be
other "today.txt" files below the current dir.

In that case I wouldn't use File::Find but a simple glob().
If standard filename expansion is not powerful enough for your needs then
you can always filter the result using grep() in a second step.

jue
 
D

david

Jürgen Exner said:
In that case I wouldn't use File::Find but a simple glob().
If standard filename expansion is not powerful enough for your needs then
you can always filter the result using grep() in a second step.

jue

Thank you. That does work better. Now my next question is, now that I
can properly find and copy my files with:

use strict;
use File::Copy;

my $targetdir="./floppy";
my $today="./current/today.txt";

open(OUT, ">>$today") || die "$!";

while ( <*.txt> ) {
copy $_, $targetdir if /today.+\.txt/i || die "Cant copy";
print OUT $_ if /today.+\.txt/i;
}

How can I iterate through each *.txt (today.+\.txt) line by line?

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top