Get list of files.

B

Binny V A

Hello Everyone,

I want to get a list of all HTML files in a folder. How do I do this?

I tried
$list = <*.htm>;
but it gave only one html file. The same result with glob("*.htm")

I am currently doing this with
$list = `dir *.htm`;
and then spliting it. But that is not portable.

Other questions - How do I get the names of folders?
How do I relusivly list all files in a folder and within folders in that folder?

Thanking You,
Binny V A
http://www.geocities.com/binnyva
Perl Special http://www.geocities.com/binnyva/code/perl
 
A

A. Sinan Unur

(e-mail address removed) (Binny V A) wrote in
Hello Everyone,

I want to get a list of all HTML files in a folder. How do I do this?

I tried
$list = <*.htm>;
but it gave only one html file. The same result with glob("*.htm")

I am currently doing this with
$list = `dir *.htm`;
and then spliting it. But that is not portable.

Other questions - How do I get the names of folders?
How do I relusivly list all files in a folder and within folders in
that folder?

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

or

use File::Find::Rule;

Sinan
 
P

Paul Lalli

Binny V A said:
I want to get a list of all HTML files in a folder. How do I do this?

I tried
$list = <*.htm>;
but it gave only one html file. The same result with glob("*.htm")

You should always read the documentation for the function you're using:
perldoc -f glob
glob EXPR
glob In list context, returns a (possibly empty) list of
filename expansions on the value of EXPR such as the
standard Unix shell /bin/csh would do. In scalar
context, glob iterates through such filename
expansions, returning undef when the list is
exhausted.

You're using a scalar context, and not attempting to iterate. You need
to do one or the other to get the full list.
Other questions - How do I get the names of folders?

Open a directory, read the directory one entry at a time, and test each
entry to determine whether or not it is a directory or a file:
perldoc -f opendir
perldoc -f readdir
perldoc -f -X
How do I relusivly list all files in a folder and within folders in
that folder?

Assuming you mean "recursively", use the standard File::Find module, or
the available-on-CPAN File::Find::Rule module.

Paul Lalli
 
G

gnari

$list = <*.htm>;
but it gave only one html file. The same result with glob("*.htm")

actually, <*.htm> does return a list of files, but you are assigning it to
a scalar, as if you had done : $x=('a','b','c');
try @list instead

gnari
 
J

Jürgen Exner

Binny said:
I want to get a list of all HTML files in a folder. How do I do this?

I tried
$list = <*.htm>;
but it gave only one html file. The same result with glob("*.htm")

1: You are asking for only one file. If you want a list of all files, then
ask for a list:
@list = <*.htm>;

2: Your verbal specification conflicts with your code. You said you want
HTML files, but in your code you are globbing htm files.
@list = said:
Other questions - How do I get the names of folders?

See "perldoc -f -X" and check in particular the -d operator.
How do I relusivly list all files in a folder and within folders in
that folder?

I guess you meant 'recursively'? See "perldoc File::Find".

jue
 
L

Leon

Binny V A said:
Hello Everyone,

I want to get a list of all HTML files in a folder. How do I do this?

I tried
$list = <*.htm>;
but it gave only one html file. The same result with glob("*.htm")
Try @list = glob("*.htm");

# Just to note that this is for *.htm files, it does not include *.html.
I am currently doing this with
$list = `dir *.htm`;
and then spliting it. But that is not portable.

Other questions - How do I get the names of folders?
Check opendir, readdir, closedir command.
How do I relusivly list all files in a folder and within folders in that
folder?
Not sure on this one, sure someone will point you in the right direction:)
 
P

Paul Lalli

gnari said:
actually, <*.htm> does return a list of files, but you are assigning it to
a scalar, as if you had done : $x=('a','b','c');

Well, not quite the same since the OP's code will assign $list to the
*first* element otherwise returned by the glob, whereas this code will
assing $x to the last element of the 'list'....

Paul Lalli
 
G

gnari

Paul Lalli said:
Well, not quite the same since the OP's code will assign $list to the
*first* element otherwise returned by the glob, whereas this code will
assing $x to the last element of the 'list'....

true.

$x=('a','b','c');
is more an example of the comma operator in action than an list
assignment.

gnari
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top