John W. Krahn wrote:
> Gan Uesli Starling wrote:
>> 
>> Am wanting to get a list of files in a directory, but only
>> files which are not themselves further sub-directories.
>> 
>> When I do this...
>> 
>>     opendir(DIR, $this_path) or die "Can't opendir $this_path: $!";
> 
> You are opening the directory with opendir() but you are not reading
> the files with readdir().
> 
>>     @file_list = glob("*");
> 
> If you use glob() then you don't have to use opendir() and closedir().
> 
> 
>>     closedir(DIR);
>> 
>> ...it gives all the files, including the files which are also
>> directories. Now on UNIX I can get a long list of all files, then
>> grep out the directories, then sed out all but the names on every
>> line. That works like so on NetBSD... 
>> 
>> $file_list = `ls -l | grep -v "^d" | sed 's/.* //g'`;
>> @file_list = split /\n/, $file_list;
>> shift @file_list;
>> 
>> ...which does exactly what I want. It lists the files which are not
>> directories. 
>> 
>> But I am writing in Perl so as to NOT be OS dependent. I looked into
>> the 
>> -d test. But it seems an awful kludge to have to open every file and
>> get a status on it.
> 
> You don't have to open the file just to use stat() or the file test
> operators.
> 
> 
>> Is there a pure Perl way to get a list of file names exclusive of any
>> sub-directory names that will work multi-platform?
> 
> Using opendir(), readdir() and closedir():
> 
> opendir DIR, $this_path or die "Can't opendir $this_path: $!";
> my @file_list = grep !-d, readdir DIR;
> closedir DIR;
> 
        Something that always catches me, is that the readdir only pulls the
directory or filename and if you have not done a chdir, then you need to do
some concatenating otherwise your tests will not give you thre results you
want. So you could make change:

@file_list = grep !-d ($this_path . $_), readdir DIR;
  where I make the $this_path have the separator / as last character

One way.

Wags ;)


**********************************************************
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.
****************************************************************


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to