read directory question 
Author Message
 read directory question


   Something like this will give you a list of all the .html files in a
   directory:

   opendir(DIR,".");


Tsk, tsk on me.  Randal pointed out that I forgot to escape the period
in the match....  Also, in case any of the files came from a PC, the
match should be htm and a possible l.

So, that should be

Steve
--

Varimetrix Corporation                          2350 Commerce Park Dr. Ste 4
Palm Bay, FL    32905                                   CAD/CAM/CAE/Software
(407) 676-3222                                           Fax: (407) 723-4388



Mon, 16 Jun 1997 23:03:17 GMT  
 read directory question
Hi all:
I want to open a directory (opendir) for reading (readdir) but I only want to read the files in the directory that  
have a certain extension such as .html.  I can't seeem to get the code right.
Any help would be sincerely appreciated.
Thanks
Jane Euler

DynCorp-Meridian



Mon, 16 Jun 1997 21:23:23 GMT  
 read directory question

Quote:

>I want to open a directory (opendir) for reading (readdir) but I only
>want to read the files in the directory that have a certain extension
>such as .html.

Try this.  To test, pipe this message to perl -x, as in
|perl -x - /some/path

--
Rodger Anderson

#!/usr/local/bin/perl -l


opendir (DIR, $path) or die "$!";

while ($_ = readdir(DIR)) {
    $_ = "$path/$_"; # Prepend full path.

    next unless -f;  # Skip non files.

    # Match file name against regular expression.
    next unless /\.html$/;

    # Do whatever with file.
    print $_;

Quote:
}

closedir (DIR);
__END__


Tue, 17 Jun 1997 04:52:13 GMT  
 read directory question
   Hi all:
   I want to open a directory (opendir) for reading (readdir) but I only want to read the files in the directory that  
   have a certain extension such as .html.  I can't seeem to get the code right.
   Any help would be sincerely appreciated.
   Thanks
   Jane Euler

How do you determine what a files name is if you don't read it in
first?  You have to read all the files and ignore the ones you don't
want.

Something like this will give you a list of all the .html files in a
directory:



Steve
--

Varimetrix Corporation                          2350 Commerce Park Dr. Ste 4
Palm Bay, FL    32905                                   CAD/CAM/CAE/Software
(407) 676-3222                                           Fax: (407) 723-4388



Mon, 16 Jun 1997 22:37:05 GMT  
 read directory question
: Hi all:
: I want to open a directory (opendir) for reading (readdir) but I only want to read the files in the directory that  
: have a certain extension such as .html.  I can't seeem to get the code right.

I believe you can say

or this ought to work:
   opendir ( dir, '.' );

   closedir ( dir );
which is probably better, but I'm not sure.
--
jan b schipmolder



Tue, 17 Jun 1997 13:27:15 GMT  
 read directory question

Quote:
>I want to open a directory (opendir) for reading (readdir) but I only want to
>read the files in the directory that have a certain extension such as .html.

If you're lazy, use a file name glob:


If you really want to use readdir (say, for efficiency reasons):

        opendir(DIR, $directory) || die "can't opendir $directory: $!\n";

        closedir(DIR);

Quote:
>I can't seeem to get the code right.

The most popular mistake with readdir is to forget that it returns
basenames only.  To read the files using the second method, you can
either chdir($directory), or use something like:


                $pathname = "$directory/$file";
                open(FH, $pathname) || die "can't open $pathname: $!\n";
                while (<FH>) {
                        #
                        # whatever ....

--
Hope this helps,

HansM



Mon, 23 Jun 1997 02:28:07 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Reading directories, entering directories, chaning file permissions...

2. CGI Module, Reading a Directory into an Array

3. Reading Directory Structure

4. Read UNC directory

5. help selecting and displaying specific files after reading directory:

6. read recursive directories

7. reading directories

8. looking for better way to read from directories

9. Read UNC directory

10. problems with reading a file from a directory

11. finding a file in a directory and reading it

12. Reading a NT directory from Unix

 

 
Powered by phpBB® Forum Software