no such file or directory?

G

Geoff Cox

Hello,

I am using File::Find and wish to use the following but ger error
"cannot open assets/i-<filename.htm> : No such file or directory at
index.pl line 67".


open OUT, ">>assets/i-$name" or die "cannot open assets/$name : $!" ;

the structure is similar to

folder1
folder2/assets
folder3

and I am guessing that the error comes because the assets directory
is not always found ? If I use simply i-$name, then the code works.

How do I correct this?

Thanks

Geoff
 
C

castillo.bryan

File::Find usually changes the current-working-directory to the
directory its scanning. So you are in the directory folder2/assets
when you see that file in your wanted function.

There is a no_chdir option you can give to find, but then you would
have to open the file as "folder2/assets/i-$name".

I would do it something like this

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use IO::File;

find({wanted => sub {
if ($File::Find::name =~ m#assets/i-.*# and -f
$File::Find::name) {
my $in = IO::File->new($File::Find::name) || die;
print("Opened $File::Find::name\n");
$in->close;
}
}, no_chdir => 1}, ".");



[maple@edieazqa01 tmp]$ ./test.pl
Opened ./folder2/assets/i-c1
Opened ./folder2/assets/i-c2
Opened ./folder2/assets/i-c3
 
G

Geoff Cox

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use IO::File;

find({wanted => sub {
if ($File::Find::name =~ m#assets/i-.*# and -f
$File::Find::name) {
my $in = IO::File->new($File::Find::name) || die;
print("Opened $File::Find::name\n");
$in->close;
}
}, no_chdir => 1}, ".");

Thanks! I have it working but must now work out how!

Cheers

Geoff


[maple@edieazqa01 tmp]$ ./test.pl
Opened ./folder2/assets/i-c1
Opened ./folder2/assets/i-c2
Opened ./folder2/assets/i-c3
 
A

Anno Siegel

File::Find usually changes the current-working-directory to the
directory its scanning. So you are in the directory folder2/assets
when you see that file in your wanted function.

There is a no_chdir option you can give to find, but then you would
have to open the file as "folder2/assets/i-$name".

I would do it something like this

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use IO::File;

find({wanted => sub {
if ($File::Find::name =~ m#assets/i-.*# and -f
$File::Find::name) {
my $in = IO::File->new($File::Find::name) || die;
print("Opened $File::Find::name\n");
$in->close;
}
}, no_chdir => 1}, ".");

Looks good, or rather, looks correct. The many repetitions of
$File::Find::name don't really look good. Topicalize (as they say)
the cumbersome name (untested):

sub {
for ( $File::Find::name ) {
if ( m#assets/i-.*# and -f ) {
my $in = IO::File->new( $_);
print "Opened $_\n";
}
}
}

close() isn't necessary because $in goes out of scope at that point.

Anno
 
J

Joe Smith

Geoff said:
I am using File::Find and wish to use the following but get error
open OUT, ">>assets/i-$name" or die "cannot open assets/$name : $!" ;

Another way would be to populate an array with just the names
of the files, and then process them after File::Find is done.

my @files;
find(sub {push @files,$File::Find::name if -f $_}, '.');
foreach my $file (@files) {
...
}

-Joe
 

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
474,266
Messages
2,571,085
Members
48,773
Latest member
Kaybee

Latest Threads

Top