For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > get only filenames and not directory names under a specific path.









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]

 

Author get only filenames and not directory names under a specific path.
Nishi Bhonsle

2006-07-21, 3:57 am

Hi:
I am starting a new thread based of an older thread just because there was a
lot of different things that were requested for and it had gotten a bit
confusing.

I got a lot of help from all you experts to write the below code that takes
an argument path C:\build\Sample\NewDir that contains--

C:\build\Sample\NewDir\File1.txt
C:\build\Sample\NewDir\File2.txt
C:\build\Sample\NewDir\NewSubDirectory
C:\build\Sample\NewDir\NewSubDirectory\1
1.txt

and prints out the following into the output file --
File1.txt
File2.txt
NewSubDirectory

use strict;
use warnings;

my $path = $ARGV[0];

opendir DIR, $path or die "Can't open $path: $!";

my @new = grep { $_ ne "." and $_ ne ".." } readdir DIR;
closedir DIR;

open FILE,">>c:/buildlist2.txt";
print FILE "$_\n" foreach @new;
close FILE;

Can I modify the above code so that no directory name is printed in the
ouputfile but only filenames are printed. ie File1.txt and File2.txt are
printed in the output file without the NewSubDirectory printed in it? I am
looking for some way that before getting the entries into the new array, i
can remove the entries that stand for directory names.

I tried using find(sub {push @new, $_ if -f}, $path); but that prints all
the filenames under NewDir as well as NewSubDirectory together which is not
what i need. For getting files under NewSubDirectory, I will issue a
separate command with argument path as
C:\build\Sample\NewDir\NewSubDirectory and hence this command does not work
since it clubs files and directories and files within sub-directories.

I tried using find(sub {push @new, $File::Find::name}, $path); but that
prints the directories and files along with their complete/absolute paths,
which is not what i need.

Thanks in advance! -Nishi.

Edi STOJICEVIC

2006-07-21, 3:57 am

Le jeu, jui 20, 2006 at 09:36:02 -0700, Nishi Bhonsle a tapoté sur son clavier :
> Hi:
> I am starting a new thread based of an older thread just because there was a
> lot of different things that were requested for and it had gotten a bit
> confusing.
>
> I got a lot of help from all you experts to write the below code that takes
> an argument path C:\build\Sample\NewDir that contains--
>
> C:\build\Sample\NewDir\File1.txt
> C:\build\Sample\NewDir\File2.txt
> C:\build\Sample\NewDir\NewSubDirectory
> C:\build\Sample\NewDir\NewSubDirectory\1
1.txt
>
> and prints out the following into the output file --
> File1.txt
> File2.txt
> NewSubDirectory
>
> use strict;
> use warnings;
>
> my $path = $ARGV[0];
>
> opendir DIR, $path or die "Can't open $path: $!";
>
> my @new = grep { $_ ne "." and $_ ne ".." } readdir DIR;
> closedir DIR;
>
> open FILE,">>c:/buildlist2.txt";
> print FILE "$_\n" foreach @new;
> close FILE;
>
> Can I modify the above code so that no directory name is printed in the
> ouputfile but only filenames are printed. ie File1.txt and File2.txt are
> printed in the output file without the NewSubDirectory printed in it? I am
> looking for some way that before getting the entries into the new array, i
> can remove the entries that stand for directory names.
>
> I tried using find(sub {push @new, $_ if -f}, $path); but that prints all
> the filenames under NewDir as well as NewSubDirectory together which is not
> what i need. For getting files under NewSubDirectory, I will issue a
> separate command with argument path as
> C:\build\Sample\NewDir\NewSubDirectory and hence this command does not work
> since it clubs files and directories and files within sub-directories.
>
> I tried using find(sub {push @new, $File::Find::name}, $path); but that
> prints the directories and files along with their complete/absolute paths,
> which is not what i need.
>
> Thanks in advance! -Nishi.



Hi,

You should look at Basename on the CPAN -->
http://search.cpan.org/~cwest/ppt-0.14/bin/basename

Regards,


--
.. ''`. (\___/) E d i S T O J I C E V I C
: :' : (='.'=) http://www.debianworld.org
`. `~' (")_(") GPG: C360 FCF0 AB3A 2AB0 52E7 044F 1B3D 2109 1237 B032
`-
Thomas J.

2006-07-21, 3:57 am

Nishi Bhonsle schrieb:
.....
>
> Can I modify the above code so that no directory name is printed in the
> ouputfile but only filenames are printed. ie File1.txt and File2.txt are
> printed in the output file without the NewSubDirectory printed in it? I am
> looking for some way that before getting the entries into the new array, i
> can remove the entries that stand for directory names.

....

simply use a filter before pushing your entries in your array.
ex.:

foreach(readdir DIR) {
next if -d $_;
push @new,$_;
}

Thomas

Rob Dixon

2006-07-21, 3:57 am

Nishi Bhonsle wrote:
> Hi:
> I am starting a new thread based of an older thread just because there
> was a
> lot of different things that were requested for and it had gotten a bit
> confusing.
>
> I got a lot of help from all you experts to write the below code that takes
> an argument path C:\build\Sample\NewDir that contains--
>
> C:\build\Sample\NewDir\File1.txt
> C:\build\Sample\NewDir\File2.txt
> C:\build\Sample\NewDir\NewSubDirectory
> C:\build\Sample\NewDir\NewSubDirectory\1
1.txt
>
> and prints out the following into the output file --
> File1.txt
> File2.txt
> NewSubDirectory
>
> use strict;
> use warnings;
>
> my $path = $ARGV[0];
>
> opendir DIR, $path or die "Can't open $path: $!";
>
> my @new = grep { $_ ne "." and $_ ne ".." } readdir DIR;
> closedir DIR;
>
> open FILE,">>c:/buildlist2.txt";
> print FILE "$_\n" foreach @new;
> close FILE;
>
> Can I modify the above code so that no directory name is printed in the
> ouputfile but only filenames are printed. ie File1.txt and File2.txt are
> printed in the output file without the NewSubDirectory printed in it? I am
> looking for some way that before getting the entries into the new array, i
> can remove the entries that stand for directory names.
>
> I tried using find(sub {push @new, $_ if -f}, $path); but that prints all
> the filenames under NewDir as well as NewSubDirectory together which is not
> what i need. For getting files under NewSubDirectory, I will issue a
> separate command with argument path as
> C:\build\Sample\NewDir\NewSubDirectory and hence this command does not work
> since it clubs files and directories and files within sub-directories.
>
> I tried using find(sub {push @new, $File::Find::name}, $path); but that
> prints the directories and files along with their complete/absolute paths,
> which is not what i need.


Just change it to

find(sub {push @new, $_}, $path);

Rob
Paul Lalli

2006-07-21, 6:57 pm

Edi STOJICEVIC wrote:
> Le jeu, jui 20, 2006 at 09:36:02 -0700, Nishi Bhonsle a tapot=E9 sur son =

clavier :

akes[color=darkred]
[color=darkred]
[color=darkred]
> You should look at Basename on the CPAN -->
> http://search.cpan.org/~cwest/ppt-0.14/bin/basename


Can you please explain what this has to do with the OPs problem? The
OP is already printing only the basename of the paths. He simply wants
to exclude entries that are directories.

Paul Lalli

Nishi Bhonsle

2006-07-21, 6:57 pm

I tried it but didnt work.
my @new;

find(sub {push @new, $_}, $path);

open FILE,">>$logfile";

print FILE "$_\n" foreach @new;
close FILE;

In addition to printing filenames and directories, it also printed the "."
The log file contained
..
file1.txt
file2.txt

How can I not include the "." ?

Thanks!

-Nishi.

On 7/21/06, Rob Dixon <rob.dixon@350.com> wrote:
>
> Nishi Bhonsle wrote:
> takes
> am
> i
> all
> not
> work
> paths,
>
> Just change it to
>
> find(sub {push @new, $_}, $path);
>
> Rob
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Wagner, David --- Senior Programmer Analyst --- WG

2006-07-21, 6:57 pm

Nishi Bhonsle wrote:
> I tried it but didnt work.
> my @new;
>=20
> find(sub {push @new, $_}, $path);

find(sub {push(@new,$_) if ( !/^\.{1,2}/ ) }, $path);

If not 1 or 2 periods push on to @new;
Wags ;)[color=darkred]
>=20
> open FILE,">>$logfile";
>=20
> print FILE "$_\n" foreach @new;
> close FILE;
>=20
> In addition to printing filenames and directories, it also printed
> the "." The log file contained
> .
> file1.txt
> file2.txt
>=20
> How can I not include the "." ?
>=20
> Thanks!
>=20
> -Nishi.
>=20
> On 7/21/06, Rob Dixon <rob.dixon@350.com> wrote:



Wags ;)
WGO: x2224

****************************************
******************************
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
****************************************
******************************

David --- Senior Programmer Analyst --- WGO Wagner

2006-07-21, 6:57 pm

Wagner, David --- Senior Programmer Analyst --- WGO wrote:
> Nishi Bhonsle wrote:
> find(sub {push(@new,$_) if ( !/^\.{1,2}/ ) }, $path);
>=20
> If not 1 or 2 periods push on to @new;
> Wags ;)


Sorry I missed your directories being printed. Depending on whether you =
want to go into the other directories looking for files that will affect =
what you want to do. If only the current directory and it's files, then =
I would use opendir and readdir. If going into other directories and =
pulling those files also then add:

and ! -d $_ to the if

Wags ;)=20
>=20
>=20
>=20
> Wags ;)
> WGO: x2224
>=20
> ****************************************
******************************
> This message contains information that is confidential and
> proprietary to FedEx Freight or its affiliates. It is intended only
> for the recipient named and for the express purpose(s) described
> therein. Any other use is prohibited.
> ****************************************
******************************




Wags ;)
WGO: x2224
Nishi Bhonsle

2006-07-21, 6:57 pm

That works like a charm, thanks!

-Nishi.


On 7/21/06, Wagner, David --- Senior Programmer Analyst --- WGO <
David.Wagner@freight.fedex.com> wrote:
>
> Nishi Bhonsle wrote:
> find(sub {push(@new,$_) if ( !/^\.{1,2}/ ) }, $path);
>
> If not 1 or 2 periods push on to @new;
> Wags ;)
>
>
>
> Wags ;)
> WGO: x2224
>
> ****************************************
******************************
> This message contains information that is confidential and proprietary to
> FedEx Freight or its affiliates. It is intended only for the recipient
> named and for the express purpose(s) described therein. Any other use is
> prohibited.
> ****************************************
******************************
>
>


DJ Stunks

2006-07-21, 6:57 pm


Nishi Bhonsle wrote:
> That works like a charm, thanks!


[TOFU snipped}

Could you PLEASE stop top posting? How am I supposed to figure out
what it was that worked for you from that mess?

-jp

PS - what is with this list lately?? or is it just me?

Dr.Ruud

2006-07-22, 7:56 am

"Wagner, David --- Senior Programmer Analyst --- WGO" schreef:

> if ( !/^\.{1,2}/ )


That would fail "..x" &c.

If you are only interested in files and not directories, use -f.

--
Affijn, Ruud

"Gewoon is een tijger."


Nishi Bhonsle

2006-07-24, 6:57 pm

Hi:
If I need to get the files without the "." and the ".." at the first level
only, then how can i modify the find command?
ie
Currently using find(sub {push(@new,$_) if ( -f ) }, $path); gives me all
files under NewDir as well as NewSubDirectory
C:\build\Sample\NewDir\File1.txt
C:\build\Sample\NewDir\File2.txt
C:\build\Sample\NewDir\NewSubDirectory
C:\build\Sample\NewDir\NewSubDirectory\1
1.txt
C:\build\Sample\NewDir\NewSubDirectory\1
2.txt

If I need to get files only under NewDir and then issue another command to
get files only under NewSubDirectory, then how can i restrict the find
command to only look for the files under the directory mentioned
i.e.

perl <progname>.pl C:\build\Sample\NewDir will only give
File1.txt
File2.txt

and

perl <progname>.pl C:\build\Sample\NewDir\NewSubDirectory will give
11.txt
12.txt

Thanks again!
-Nishi.


On 7/22/06, Dr.Ruud <rvtol+news@isolution.nl> wrote:
>
> "Wagner, David --- Senior Programmer Analyst --- WGO" schreef:
>
>
> That would fail "..x" &c.
>
> If you are only interested in files and not directories, use -f.
>
> --
> Affijn, Ruud
>
> "Gewoon is een tijger."
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Dr.Ruud

2006-07-24, 9:56 pm

"Nishi Bhonsle" schreef:

> If I need to get the files without the "." and the ".." at the first
> level only, then how can i modify the find command?
> ie
> Currently using find(sub {push(@new,$_) if ( -f ) }, $path); gives
> me all files under NewDir as well as NewSubDirectory
> C:\build\Sample\NewDir\File1.txt
> C:\build\Sample\NewDir\File2.txt
> C:\build\Sample\NewDir\NewSubDirectory
> C:\build\Sample\NewDir\NewSubDirectory\1
1.txt
> C:\build\Sample\NewDir\NewSubDirectory\1
2.txt
>
> If I need to get files only under NewDir and then issue another
> command to get files only under NewSubDirectory, then how can i
> restrict the find command to only look for the files under the
> directory mentioned


See `perldoc -f readdir`.

But I would use IO::All
http://search.cpan.org/search?module=IO::All

--
Affijn, Ruud

"Gewoon is een tijger."


Nishi Bhonsle

2006-07-24, 9:56 pm

Hi:
I am not able to get the files at just the first level.
ie
print "$_\n" for $io->all(0); prints all level down
and
print "$_\n" for $io->all(1); prints the files and the dir name at the
first level
ie
issuing the command for path C:\build\Sample\NewDir
prints the below
C:\build\Sample\NewDir\File1.txt
C:\build\Sample\NewDir\File2.txt
C:\build\Sample\NewDir\NewSubDirectory

In my case, I am not interested in the directory listing but only file
listing.

I didnt find anything in IO:All that would give me only file listing under a
particular directory without including the subdirectory listing.

Thanks, Nishi.


On 7/24/06, Dr.Ruud <rvtol+news@isolution.nl> wrote:
>
> "Nishi Bhonsle" schreef:
>
>
> See `perldoc -f readdir`.
>
> But I would use IO::All
> http://search.cpan.org/search?module=IO::All
>
> --
> Affijn, Ruud
>
> "Gewoon is een tijger."
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Mumia W.

2006-07-24, 9:56 pm

On 07/24/2006 06:35 PM, Nishi Bhonsle wrote:
> Hi:
> If I need to get the files without the "." and the ".." at the first level
> only, [...]


use File::Slurp;
my @files = read_dir('mydirectory');

Do you have the perl documentation installed?

This should give you information about File::Slurp:

perldoc File::Slurp

Please let us know if you find the document.


Dr.Ruud

2006-07-24, 9:56 pm

"Nishi Bhonsle" schreef:

> I am not able to get the files at just the first level.
> ie
> print "$_\n" for $io->all(0); prints all level down
> and
> print "$_\n" for $io->all(1); prints the files and the dir name at
> the first level


"All" and "all(0)" are the same.
"all" and "all(1)" are the same.

Try "all_files", which should be the same as "all_files(1)".

It's all in the documentation...

--
Affijn, Ruud

"Gewoon is een tijger."


Rob Dixon

2006-07-25, 7:57 am

Nishi Bhonsle wrote:
>
> I am not able to get the files at just the first level.
> ie
> print "$_\n" for $io->all(0); prints all level down
> and
> print "$_\n" for $io->all(1); prints the files and the dir name at the
> first level
> ie
> issuing the command for path C:\build\Sample\NewDir
> prints the below
> C:\build\Sample\NewDir\File1.txt
> C:\build\Sample\NewDir\File2.txt
> C:\build\Sample\NewDir\NewSubDirectory
>
> In my case, I am not interested in the directory listing but only file
> listing.
>
> I didnt find anything in IO:All that would give me only file listing
> under a particular directory without including the subdirectory listing.


Hi Nishi.

We seem to be going round in circles here. I suggested File::Find because I
thought you wanted to find stuff nested in a directory structure. If all you
want is a list of plain files in a directory you don't need any modules at all.
Try:

use strict;
use warnings;

my @dir = do {
opendir my $dh, 'C:\build\Sample\NewDir' or die $!;
grep -f, readdir $dh;
};

print "$_\n" foreach @dir;

Is this what you want?

Rob
Dr.Ruud

2006-07-25, 7:57 am

Rob Dixon schreef:

> use strict;
> use warnings;
>
> my @dir = do {
> opendir my $dh, 'C:\build\Sample\NewDir' or die $!;
> grep -f, readdir $dh;


For newbies, maybe put a comment here that the closedir() is implicit.

> };
>
> print "$_\n" foreach @dir;



--
Affijn, Ruud

"Gewoon is een tijger."


Nishi Bhonsle

2006-07-25, 6:57 pm

Yes, all_files works. Its in the doc, and i missed it. Its my bad.

Rob, I tried the code snippet you mentioned, apparently it doesnot return
anything under the directory.

Thanks, Nishi.

On 7/25/06, Dr.Ruud <rvtol+news@isolution.nl> wrote:
>
> Rob Dixon schreef:
>
>
> For newbies, maybe put a comment here that the closedir() is implicit.
>
>
>
> --
> Affijn, Ruud
>
> "Gewoon is een tijger."
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Nishi Bhonsle

2006-07-25, 6:57 pm

Hi:

In addition can i modify the all_files to return only the absolute filename
and not the filename alongwith the entire path?
Currently, it prints
C:\..\..\..\File1.txt
etc
I really want it only print File1.txt

Thanks!


On 7/25/06, Nishi Bhonsle <nishiandprafull@gmail.com> wrote:
>
>
> Yes, all_files works. Its in the doc, and i missed it. Its my bad.
>
> Rob, I tried the code snippet you mentioned, apparently it doesnot return
> anything under the directory.
>
> Thanks, Nishi.
>
> On 7/25/06, Dr.Ruud <rvtol+news@isolution.nl> wrote:
>


Nishi Bhonsle

2006-07-25, 6:57 pm

I got the last soln, ie to use File::Basename module. Please ignore the last
thread.

I am still interested to know why Rob's soln doesnt work for me?

Thanks much!


On 7/25/06, Nishi Bhonsle <nishiandprafull@gmail.com> wrote:
>
>
> Hi:
>
> In addition can i modify the all_files to return only the absolute
> filename and not the filename alongwith the entire path?
> Currently, it prints
> C:\..\..\..\File1.txt
> etc
> I really want it only print File1.txt
>
> Thanks!
>
>
> On 7/25/06, Nishi Bhonsle <nishiandprafull@gmail.com> wrote:
>


Rob Dixon

2006-07-25, 6:57 pm

Nishi Bhonsle wrote:
>
> I got the last soln, ie to use File::Basename module. Please ignore the
> last thread.
>
> I am still interested to know why Rob's soln doesnt work for me?


So was I, and it's because I made a mistake. Because the values returned by
readdir don't include a full path, the file test only works if the directory
being listed is the current one. Try this version instead with my humble
apologies.

Rob


use strict;
use warnings;

my $path = 'C:\build\Sample\NewDir';

my @dir = do {
opendir my $dh, $path or die $!;
grep -f "$path\\$_", readdir $dh;
};

print "$_\n" foreach @dir;

Dr.Ruud

2006-07-25, 9:57 pm

"Nishi Bhonsle" schreef:

[don't! top-post!]

> [IO:All]
> Currently, it prints
> C:\..\..\..\File1.txt
> etc
> I really want it only print File1.txt


I don't understand how you read the documentation. Just search for
'filename'.

And start to grok the 'All' in IO::All.

--
Affijn, Ruud

"Gewoon is een tijger."


Alan_C

2006-07-25, 9:57 pm

On Tuesday 25 July 2006 15:56, Rob Dixon wrote:
> Nishi Bhonsle wrote:
>
> So was I, and it's because I made a mistake. Because the values returned by
> readdir don't include a full path, the file test only works if the
> directory being listed is the current one. Try this version instead with my
> humble apologies.

[ snip ]

I'd experimented *before* I saw this post.

#!/usr/bin/perl
use strict;
use warnings;

my @dir = do {
# opendir my $dh, 'C:\build\Sample\NewDir' or die $!;
opendir my $dh, '/home/al/temp4' or die $!;
# grep -f, readdir $dh;
readdir $dh;
};

print "$_\n" foreach @dir;
# print @dir, "\n";


That works on Linux (not matters the current dir, I was in /home/al when I ran
it) which leads me to guess that something be wrong with the grep line.

Also, on Windows, doesn't it need to be either C:\\build\\Sample\\NewDir

or

C:/build/Sample/NewDir

In my past Win days, I used to do it like that latter.

I'm guess maybe it's must do that when it's double quotes. I see you used
single quotes.

--
Alan.
Rob Dixon

2006-07-26, 3:57 am

Hello Alan

Alan_C wrote:
>
> On Tuesday 25 July 2006 15:56, Rob Dixon wrote:
>
>
> [ snip ]
>
> I'd experimented *before* I saw this post.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @dir = do {
> # opendir my $dh, 'C:\build\Sample\NewDir' or die $!;
> opendir my $dh, '/home/al/temp4' or die $!;
> # grep -f, readdir $dh;
> readdir $dh;
> };
>
> print "$_\n" foreach @dir;
> # print @dir, "\n";


At least use

print "@dir\n"

or the filenames will all be concatenated together without being able to tell
where they start and end.

> That works on Linux (not matters the current dir, I was in /home/al when I ran
> it) which leads me to guess that something be wrong with the grep line.


Exactly, and I fixed it in the part of my post that you snipped! The
requirements of the OP were that directories be excluded from the list and that
only the filenames be present without their paths. readdir() returns just the
filenames, but the error in my code was that without a full path the -f operator
will look for the file in the current directory to decide whether it is a plain
file or not instead of in its proper place. Files were being filtered out of the
list because they didn't exist at all (in the current directory), not because
they were the unwanted directories. Your code succeeds because you have removed
the grep() but now it no longer fulfils the requirement because all the
directories are in there as well.

> Also, on Windows, doesn't it need to be either C:\\build\\Sample\\NewDir
>
> or
>
> C:/build/Sample/NewDir
>
> In my past Win days, I used to do it like that latter.
>
> I'm guess maybe it's must do that when it's double quotes. I see you used
> single quotes.


Exactly. Perl won't try to interpolate the contents of singly-quoted strings and
lone backslashes are fine, but with two exceptions: the escape character itself
can be escaped to 'protect' it, so a double backslash in the source will produce
a single one in the resulting string, just the same as within double-quotes; and
the string delimiter can also be escaped to allow embedded single quotes, which
means that a path with a trailing backslash needs two backslashes there to avoid
losing the closing quote. So:

'C:\WINDOWS'

but

'C:\WINDOWS\'

and hence a lone backslash always needs doubling, whether in single or double
quotes:

'\' or "\\"

Perl, of course, is quite happy whether you use forward or backward slashes as
the path separator, and certainly forward slashes cause less hassle. The problem
comes in Windows if you need to interact with external software, which will
expect and supply backslashes in all its paths. You then have to choose between
using backslashes throughout or translating to and from the friendlier forward
slash before the data is allowed into or out of the Perl environment.

HTH,

Rob
Alan_C

2006-07-27, 6:57 pm

On Wednesday 26 July 2006 00:59, Rob Dixon wrote:
> Hello Alan
>
> Alan_C wrote:
[ snip ][color=darkred]
[ snip ][color=darkred]
[ snip ][color=darkred]
[ snip ][color=darkred]
> but the error in my code was that without a full path
> the -f operator will look for the file in the current directory to decide
> whether it is a plain file or not instead of in its proper place. Files
> were being filtered out of the list because they didn't exist at all (in
> the current directory), not because they were the unwanted directories.
> Your code succeeds because you have removed the grep() but now it no longer


Ok, *now* I get it.

my @dir = do {
# opendir my $dh, 'C:\build\Sample\NewDir' or die $!;
opendir my $dh, '/home/al/temp4' or die $!;
# grep -f, readdir $dh;

Therein, the grep works on the $dh (/home/al/temp4) while the -f operator
works on the current dir [not (sp) necisarily /home/al/temp4]

Thus the fixed version has the full path in the grep line (or, obviously,
could first change dir to desired dir before doing the grep -f)

I tinkered with it a bit, added a file filter capability.

#!/usr/bin/perl
use strict;
use warnings;

my $path = '/home/al/temp4'; # c:/multi/platform

my @dir = do {
opendir my $dh, $path or die $!;
grep -f "$path/$_", readdir $dh;
};

# print "$_\n" foreach @dir; # all files

foreach ( @dir ) {
print $_, "\n" if /\.htm/i; # my file filter
}# end

--
Alan.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com