Array of file names

M

MM

Hello,

Does anyone know a nice way to create an array with names of the files in a
path? That is, each element in the array is a name (just the name!) of one
of the files in the path.

A short example.

The path have these files:

-rw-r--r-- 1 john john 111 Sep 10 12:52 e_out2.log
-rw-r--r-- 1 john john 125 Sep 10 12:52 e_out.log
-rw-r--r-- 1 john john 182 Sep 10 12:52 p_out2.log
-rw-r--r-- 1 john john 210 Sep 10 12:52 p_out.log
-rw-r--r-- 1 john john 1050679 Sep 10 12:52 test2.prn
-rw-r--r-- 1 john john 1050679 Sep 10 12:52 test2.ps
-rw-r--r-- 1 john john 1050795 Sep 10 12:52 test.prn
-rw-r--r-- 1 john john 5426813 Sep 10 12:52 test.ps

Then I want the array, say $Files, to be like this:

$Files[0] = e_out2.log
$Files[1] = e_out.log
$Files[2] = p_out2.log
$Files[3] = p_out.log
$Files[4] = test2.prn
$Files[5] = test2.ps
$Files[6] = test.prn

$Files[7] = test.ps

Many thanks in advance,

MM
 
G

Gunnar Hjalmarsson

MM said:
Does anyone know a nice way to create an array with names of the
files in a path? That is, each element in the array is a name (just
the name!) of one of the files in the path.

perldoc -f readdir
 
T

Tony Curtis

Hello, Does anyone know a nice way to create an array with
names of the files in a path? That is, each element in the
array is a name (just the name!) of one of the files in the
path.

perldoc -f opendir
perldoc -f readdir
perldoc -f grep

hth
t
 
H

Helgi Briem

Does anyone know a nice way to create an array with names of the files in a
path? That is, each element in the array is a name (just the name!) of one
of the files in the path.

my $dir = '/full/path/to/directory';

opendir DIR, $dir or die "Cannot open directory $dir:$!\n";

# leave out . and ..
my @filenames = grep !/\.{1,2}$/,readdir DIR;
closedir DIR;

print join "\n", @filenames;

--
Helgi Briem hbriem AT simnet DOT is

Never worry about anything that you see on the news.
To get on the news it must be sufficiently rare
that your chances of being involved are negligible!
 
M

MM

Helgi Briem said:
my $dir = '/full/path/to/directory';

opendir DIR, $dir or die "Cannot open directory $dir:$!\n";

# leave out . and ..
my @filenames = grep !/\.{1,2}$/,readdir DIR;
closedir DIR;

print join "\n", @filenames;

Ah, very nice! Just perfect. Many thanks!

MM
 
M

Michael Slass

MM said:
Hello,

Does anyone know a nice way to create an array with names of the files in a
path? That is, each element in the array is a name (just the name!) of one
of the files in the path.

If the point of making the array is to then do something with each
file, I'd take a look at the File::Find module.

perldoc File::Find
 
T

Tore Aursand

Does anyone know a nice way to create an array with names of the files
in a path? That is, each element in the array is a name (just the name!)
of one of the files in the path.

Use the File::Find::Rule module, as it's quite useful (not tested);

#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;
use File::Find::Rule;

my $dir = '/some/directory';
my @files = File::Find::Rule->file()->relative()->in( $dir );

print Dumper( \@files );

Please read the documentation for more details;

perldoc File::Find::Rule

This module - and many other - is available from CPAN;

<http://www.cpan.org/>
 

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,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top