home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!gatech!destroyer!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!mmm.serc.3m.com!web!timbuk.cray.com!hemlock.cray.com!roehrich
- From: roehrich@cray.com (Dean Roehrich)
- Subject: Re: # of files in directory
- Message-ID: <1992Jul22.150613.26006@hemlock.cray.com>
- Keywords: Files, Perl, Directory, ls
- Lines: 26
- Organization: Cray Research, Inc.
- References: <1992Jul22.182940.1248@chpc.org>
- Date: 22 Jul 92 15:06:13 CDT
-
- In article <1992Jul22.182940.1248@chpc.org> shishir@chpcchpc.org (Shishir Gundavaram) writes:
- >Can anyone please tell me if there an easy way in Perl to determine
- >the number of files in a particular directory.
- >
- >One way I can think of to do this is:
- >
- > opendir (FILE, "/usr/bin");
- > @some_array = grep (!/^\./, readdir (FILE));
- > $number = $#some_array + 1;
- > print "There are $number of files!";
-
-
- You can throw out the $number stuff. Remember that $#array gives you the
- subscript of the last element while scalar( @array ) gives you the length of
- the array. (Also, you did not compensate for $[.)
-
- Replace your last 2 lines with:
- print "There are ", scalar @some_array, " files!\n";
-
- If you're looking for speed, use 'while' rather than 'grep'. The code may
- look more mundane but on a large directory it'll probably blow your grep
- away.
-
-
- Dean Roehrich
- roehrich@cray.com
-