glob function and getting directory contents

A

Abhinav

Hi,

I have a somewhat trivial problem, on which some input would be highly
appreciated.

I want to get the list of all the files in the current directory, whose
names are in the format

[0-9]+.xml
i.e. all files whose names are of the format <positive integer>.xml

I was hoping to do it with glob(), but I cannot come up with a pattern
which will be able to do this.

In fact, it seems (according to responses to my thread on
comp.os.linux.misc) that this is not possible ?

Would appreciate any pointers on how this could be done.

Regards
 
P

Paul Lalli

Hi,

I have a somewhat trivial problem, on which some input would be highly
appreciated.

I want to get the list of all the files in the current directory, whose
names are in the format

[0-9]+.xml
i.e. all files whose names are of the format <positive integer>.xml

I was hoping to do it with glob(), but I cannot come up with a pattern
which will be able to do this.

In fact, it seems (according to responses to my thread on
comp.os.linux.misc) that this is not possible ?

Why do you want to use glob?

open my $dir, '.' or die "Cannot open current directory: $!";
my @files = grep /^\d+\.xml$/, readdir($dir);


If you really want to use glob, I suppose you could also do:
my @files = grep /^\d+\.xml$/, glob('*');

Paul Lalli
 
A

Abhinav

Paul said:
Hi,

I have a somewhat trivial problem, on which some input would be highly
appreciated.

I want to get the list of all the files in the current directory, whose
names are in the format

[0-9]+.xml
i.e. all files whose names are of the format <positive integer>.xml

I was hoping to do it with glob(), but I cannot come up with a pattern
which will be able to do this.

In fact, it seems (according to responses to my thread on
comp.os.linux.misc) that this is not possible ?


Why do you want to use glob?

Because I thought that this would be the simplest way to do it. But maybe not
open my $dir, '.' or die "Cannot open current directory: $!";
my @files = grep /^\d+\.xml$/, readdir($dir);


If you really want to use glob, I suppose you could also do:
my @files = grep /^\d+\.xml$/, glob('*');

Thanks ! This will do well enough !

Regards
 
P

Paul Lalli

Paul said:
Hi,

I have a somewhat trivial problem, on which some input would be highly
appreciated.

I want to get the list of all the files in the current directory, whose
names are in the format

[0-9]+.xml
i.e. all files whose names are of the format <positive integer>.xml

I was hoping to do it with glob(), but I cannot come up with a pattern
which will be able to do this.

In fact, it seems (according to responses to my thread on
comp.os.linux.misc) that this is not possible ?


Why do you want to use glob?

Because I thought that this would be the simplest way to do it. But maybe not

glob is useful for filename expansion, as the csh shell would do it. That
is, it will expand '~mritty' to /users/mritty or "*.c" to a list of all
files ending in .c. If you have anything more complex than that, I'd
recommend using Perl's RegExps, which are far more powerful.
Thanks ! This will do well enough !

Quite Welcome.

Paul Lalli
 
T

Tintin

Paul Lalli said:
Hi,

I have a somewhat trivial problem, on which some input would be highly
appreciated.

I want to get the list of all the files in the current directory, whose
names are in the format

[0-9]+.xml
i.e. all files whose names are of the format <positive integer>.xml

I was hoping to do it with glob(), but I cannot come up with a pattern
which will be able to do this.

In fact, it seems (according to responses to my thread on
comp.os.linux.misc) that this is not possible ?

Why do you want to use glob?

open my $dir, '.' or die "Cannot open current directory: $!";
my @files = grep /^\d+\.xml$/, readdir($dir);


If you really want to use glob, I suppose you could also do:
my @files = grep /^\d+\.xml$/, glob('*');

or a variation on that:

foreach my $file (<*.xml>) {
next unless $file =~ /^\d+\.xml$/;
..
}
 

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
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top