Help with globbing

M

Mr. Land

Hello.

I've read what I could find on filename globbing and can't seem to get
an answer for the following:

I have directory containing files like:

File One.red
File One.blue
File One.green3
File One.Green4
File One.Gray4
SecondFile.red
SecondFile.blue
SecondFile.green4

I have built an array containing the "basenames" (everything up to but
not including the last ".") of
all files in the directory. For my example above, my array would
contain 2 elements:

"File One"
"SecondFile"

Now, for each such array entry I am trying to get an array of
filenames which match a certain pattern,
that pattern being based on the entry. For example to get a list of
all files having the basename
and having an extension that begins with either "g" or "G" and ends in
"4":

foreach $basename (@TheArray) {

@fileList = <$basename*[gG]*4>;

This worked as shown for "SecondFile" but failed with "File One" due
to the embedded space. I tried
various forms of quoting, such as:

@fileList = <'$basename'*[gG]*4>;

@fileList = <'$basename*[gG]*4'>;

@fileList = <"$basename*[gG]*4">;

but none of these seem to handle BOTH cases (with and without embedded
spaces in the basename).

Is there at least one way to do this and what would that look like?

Thanks.
 
M

Michael Slass

Hello.

I've read what I could find on filename globbing and can't seem to get
an answer for the following:

I have directory containing files like:

File One.red
File One.blue
File One.green3
File One.Green4
File One.Gray4
SecondFile.red
SecondFile.blue
SecondFile.green4

I have built an array containing the "basenames" (everything up to but
not including the last ".") of
all files in the directory. For my example above, my array would
contain 2 elements:

"File One"
"SecondFile"

Now, for each such array entry I am trying to get an array of
filenames which match a certain pattern,
that pattern being based on the entry. For example to get a list of
all files having the basename
and having an extension that begins with either "g" or "G" and ends in
"4":

foreach $basename (@TheArray) {

@fileList = <$basename*[gG]*4>;

This worked as shown for "SecondFile" but failed with "File One" due
to the embedded space. I tried
various forms of quoting, such as:

@fileList = <'$basename'*[gG]*4>;

@fileList = <'$basename*[gG]*4'>;

@fileList = <"$basename*[gG]*4">;

but none of these seem to handle BOTH cases (with and without embedded
spaces in the basename).

Is there at least one way to do this and what would that look like?

Thanks.

I think you'd have more success using perl's full regular expressions
than with globbing, which is designed to be like the UNIX shells' *
operator, and is a fairly blunt tool.

The canonical perl way to do this is by opening a directory handle,
and then iterating over the files in the directory, processing each
one.

------------------------------ cut here ------------------------------
#!/bin/perl/bin/perl -w
use strict;

my $fileName;
opendir(DIR, "/path/to/directory");

foreach $fileName (readdir DIR)
{
# match File One.[gG]<anything>4
if ( $fileName =~ m/File One\.[gG].*4/ ) {
# do something, like add it to a list for later processing
# push @someListVar, $fileName
;
# match SecondFile.[bB]
} elsif ( $fileName =~ m/SecondFile\.[bB]/ ) {
# do something with this file anem
}
# and so on
}

closedir(ETC);
------------------------------ cut here ------------------------------

You should read about dirhandles and regular expressions in the perl
docs:

perldoc -f opendir
perldoc -f readdir
perldoc -f closedir

perldoc perlre
(this last one is the page for how perl regular expressions work, and
it's big)
 
C

chris-usenet

Michael Slass said:
# match File One.[gG]<anything>4
if ( $fileName =~ m/File One\.[gG].*4/ ) {

This also will match file names like "Not File One.gb4" and
"File One.gotlotsmore4u.txt".

Chris
 
M

Mr. Land

John W. Krahn said:
Yes you should.

my @fileList = grep /\Q$baseString\E.*[gG].*4$/, readdir DIR;


John

I've not yet been exposed to that "\Q", "\E" syntax ... I'll have a
look, thanks very much for your help.
 
M

Mr. Land

Michael Slass said:
# match File One.[gG]<anything>4
if ( $fileName =~ m/File One\.[gG].*4/ ) {

This also will match file names like "Not File One.gb4" and
"File One.gotlotsmore4u.txt".

Chris

I should have mentioned that the "File One" part is going to be in a
variable, and it's already going to contain everything up to but not
including the last period in the full filename. Thanks very much for
your reply.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top