using -e with array of filenames

N

Ninja67

I've been using...
-e "myfile.txt"

to determine if a file exists and then I use...
unlink "myfile.txt"

to delete the file.

According to the documentation, I can use an array of filenames with
unlink so that I could delete multiple files at a time. Can I do
something similar with the -e?

In other words, can I check for the existance of multiple files without
writing a lengthy if statement such as the one below:

if (-e $done_dir."MMBTRVCF.txt" && -e $done_dir."MMBTRC2F.txt" && -e
$done_dir."MMBTRVLF.txt" && -e $done_dir."MMBTRL2F.txt" && -e
$done_dir."MMBTRVPF.txt" && -e $done_dir."MMBTRP2F.txt" && -e
$done_dir."CREATE_DATA.txt") {
....
 
E

Eric Schwartz

Ninja67 said:
I've been using...
-e "myfile.txt"

to determine if a file exists and then I use...
unlink "myfile.txt"

to delete the file.

According to the documentation, I can use an array of filenames with
unlink so that I could delete multiple files at a time. Can I do
something similar with the -e?

If you read the documentation ("perldoc -f -e" or "perldoc -f -X"),
you'd see:

This unary operator takes one argument, either a filename or a
file- handle, and tests the associated file to see if something
is true about it.

So no, you can't.
In other words, can I check for the existance of multiple files without
writing a lengthy if statement such as the one below:

if (-e $done_dir."MMBTRVCF.txt" && -e $done_dir."MMBTRC2F.txt" && -e
$done_dir."MMBTRVLF.txt" && -e $done_dir."MMBTRL2F.txt" && -e
$done_dir."MMBTRVPF.txt" && -e $done_dir."MMBTRP2F.txt" && -e
$done_dir."CREATE_DATA.txt") {
...

You can just build an array and delete it all at once:

my @filenames = qw/ .... /;
my @del_list;
for my $file (@filenames) {
push @del_list, $file if -e $file;
}
unlink @del_list;

Also read perlop about qw and friends for specifying a list in-place.
I generally find it easier to build up the list somewhere else, if
it's reasonably large.

-=Eric
 
X

xhoster

Ninja67 said:
According to the documentation, I can use an array of filenames with
unlink so that I could delete multiple files at a time. Can I do
something similar with the -e?

my @new_list = grep -e, @old_list;

Xho
 
A

A. Sinan Unur

to determine if a file exists and then I use...
unlink "myfile.txt"

to delete the file.

There is no need to check for existence before attempting to delete.
 
J

Joe Smith

Ninja67 said:
I've been using...
-e "myfile.txt"

to determine if a file exists and then I use...
unlink "myfile.txt"

to delete the file.

According to the documentation, I can use an array of filenames with
unlink so that I could delete multiple files at a time. Can I do
something similar with the -e?

I would recommend against using unlink with an array, since
that interferes with outputting informative error messages.

foreach my $file (@files) {
if (-e $file) {
unlink $file or warn "Could not delete $file: $!\n";
} else {
print "File $file has already been deleted\n";
}
}

or

foreach my $file (@files) {
next unless -e $file; # Quietly skip deleted files
unlink $file or warn "Could not delete $file: $!\n";
}


-Joe
 
B

Brian McCauley

Joe said:
I would recommend against using unlink with an array, since
that interferes with outputting informative error messages.

foreach my $file (@files) {
if (-e $file) {
unlink $file or warn "Could not delete $file: $!\n";
} else {
print "File $file has already been deleted\n";
}
}

or

foreach my $file (@files) {
next unless -e $file; # Quietly skip deleted files
unlink $file or warn "Could not delete $file: $!\n";
}

I think it's possibly more appropriate do the unlink unconditionally and
then ignore ENOENT.

use Errno;
foreach my $file (@files) {
unless ( unlink $file or $!{ENOENT} ) {
warn "Could not delete $file: $!\n";
}
}
 
A

Anno Siegel

Eric Schwartz said:
Ninja67 said:
I've been using...
-e "myfile.txt"

to determine if a file exists and then I use...
unlink "myfile.txt"

to delete the file.
[...]

You can just build an array and delete it all at once:

my @filenames = qw/ .... /;
my @del_list;
for my $file (@filenames) {
push @del_list, $file if -e $file;
}
unlink @del_list;

unlink grep -e, qw/ ... /;

But then, why even bother. "unlink" silently passes non-existent files:

unlink qw/ ... /;

Anno
 
E

Eric Schwartz

unlink grep -e, qw/ ... /;

For some reason, I keep forgetting about grep; I'm fine with map, I
use it all the time, but grep for some reason just doesn't always
stick with me. Part of it, I think, is the project I'm on has several
people who are very uncomfortable with "weird" (i.e., not like ksh)
aspects of Perl, so I try to keep the syntax minimal.
But then, why even bother. "unlink" silently passes non-existent files:

unlink qw/ ... /;

One advantage to grep is it's easier to debug; if you have the grep'd
filenames in an array, you can print them out to verify they're the
complete set, or more easily add other operations-- for instance,
logging the deletes in a database or logfile somewhere.

-=Eric
 
A

Anno Siegel

Eric Schwartz said:
For some reason, I keep forgetting about grep; I'm fine with map, I
use it all the time, but grep for some reason just doesn't always
stick with me. Part of it, I think, is the project I'm on has several
people who are very uncomfortable with "weird" (i.e., not like ksh)
aspects of Perl, so I try to keep the syntax minimal.

I can see where you're standing, but it's a pity, really. The grep
and map functions, along with slices and the occasional splice are
the functions that treat arrays or lists, as the case may be, as units.
They shift the attention to the aggregate instead of to its elements.
In many situations, that is a good thing.

I bet you have to be stingy with refs too.
One advantage to grep is it's easier to debug; if you have the grep'd
filenames in an array, you can print them out to verify they're the
complete set, or more easily add other operations-- for instance,
logging the deletes in a database or logfile somewhere.

Oh, sure, including a quick check if it's done its duty (untested):

my @goners = grep -f ...;
@goners == unlink @goners or warn "some remain...";

....or even

@$_ == unlink @$_ or warn "some remain..." for [ grep -f ...];

Anno
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top