Home > Archive > PERL Beginners > January 2006 > file test
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| dear all,
i am trying to check if there are any files named file* (e.g.
file_001.txt file1.doc) in my directory.
if( -e "file*" ) { print("true\n"); } # this doesn't work
else { print("false\n"); }
if( -e "m/^file/" ) { print("true\n"); } # this doesn't work either
else { print("false\n"); }
thank you in advance, any answer would be greatly appreciated.
-H.
| |
| Chris Devers 2006-01-23, 9:55 pm |
| On Mon, 23 Jan 2006, hien wrote:
> i am trying to check if there are any files named file* (e.g.
> file_001.txt file1.doc) in my directory.
perldoc -f glob
--
Chris Devers
DO NOT LEAVE IT IS NOT REAL
| |
| Gerard Robin 2006-01-24, 7:55 am |
| On Mon, Jan 23, 2006 at 09:47:40PM +0100, hien wrote:
> i am trying to check if there are any files named file* (e.g.
> file_001.txt file1.doc) in my directory.
#!/usr/bin/perl
use warnings;
use strict;
my $i = 0;
while (</home/yourlogin/file*> ) {
if (-e $_) {
print "$_ exists\n";
$i++;
}
}
print "there are $i files\n";
hth
--
Gérard
| |
| Bob Showalter 2006-01-24, 7:55 am |
| hien wrote:
> dear all,
>
> i am trying to check if there are any files named file* (e.g.
> file_001.txt file1.doc) in my directory.
>
> if( -e "file*" ) { print("true\n"); } # this doesn't work
> else { print("false\n"); }
>
> if( -e "m/^file/" ) { print("true\n"); } # this doesn't work either
> else { print("false\n"); }
Ouch, there's the dreaded "doesn't work"! (c.f.
http://perl.plover.com/Questions4.html) Fortunately, you told us what
you are after, so the question can be answered.
-e only takes a single filename.
You need a glob:
print "Found!" if glob('file*');
Or the slightly deprecated:
print "Found!" if <file*>;
| |
| Adriano Rodrigues Ferreira 2006-01-24, 6:56 pm |
|
The operator -e (like the other -X operators) takes a filename or file
handle, and not a glob or file name pattern with wildcards. As others
pointed, you use the builtin glob to do the expansion (either explicitly
or via <> ).
Then you might say:
for (glob 'file*') {
print "true\n" if -e $_;
}
Note that if the glob was expanded, it is generally obvious that the file
exists (that is, -e $_ above will be true above). But if no filename
matching 'file*' was found, the function return 'file*' unchanged.
Regards,
Adriano.
--
dear all,
i am trying to check if there are any files named file* (e.g.
file_001.txt file1.doc) in my directory.
if( -e "file*" ) { print("true\n"); } # this doesn't work
else { print("false\n"); }
if( -e "m/^file/" ) { print("true\n"); } # this doesn't work either
else { print("false\n"); }
thank you in advance, any answer would be greatly appreciated.
-H.
| |
| Adriano Rodrigues Ferreira 2006-01-24, 6:56 pm |
|
The operator -e (like the other -X operators) takes a filename or file
handle, and not a glob or file name pattern with wildcards. As others
pointed, you use the builtin glob to do the expansion (either explicitly
or via <> ).
Then you might say:
for (glob 'file*') {
print "true\n" if -e $_;
}
Note that if the glob was expanded, it is generally obvious that the file
exists (that is, -e $_ above will be true above). But if no filename
matching 'file*' was found, the function return 'file*' unchanged.
Regards,
Adriano.
--
We don't need no stinking Java - we want to get some fun.
| |
| Paul Lalli 2006-01-24, 6:56 pm |
| Adriano Rodrigues Ferreira wrote:
> for (glob 'file*') {
> print "true\n" if -e $_;
> }
>
> Note that if the glob was expanded, it is generally obvious that the file
> exists (that is, -e $_ above will be true above). But if no filename
> matching 'file*' was found, the function return 'file*' unchanged.
Can you please explain what gave you that impression? Are you,
perhaps, thinking of shell-script filename expansion? This simply does
not hold for Perl's glob():
$ ls
file.pl file.txt file2.pl
$ perl -le'print for glob q{*.pl}'
file.pl
file2.pl
$ perl -le'print for glob q{*.txt}'
file.txt
$ perl -le'print for glob q{file*}'
file.pl
file.txt
file2.pl
$ perl -le'print for glob q{*.asdf}'
$
Paul Lalli
|
|
|
|
|