Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count no. of files

Status
Not open for further replies.

sappleg

Programmer
Apr 15, 2008
31
CA
Hi Guys,

I have a script where I need to count the no. of files in a given directory and have an output something like this:

"Total no. of files: nn"

How to achieve this please? I am new to Perl.
 
Is this a windows or *nix based system? I *nix based system, u can simply ls -l | wc -l to get a count of file (not hidden) or ls -al | wc -l (to get hidden files as well).
For windows based, u need to a script for, which I haven't tried yet.

Have you done any research or tried anything so far?
 
Untested code:

Code:
opendir DIR, 'path/to/dir' or die "$!";
my @files = grep {-f "path/to/dir/$_"} readdir DIR;
close DIR;
print "Total no. of files: ", scalar @files, "\n";




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you guys. I have achieved this by using the following code:

my @files = <$ftp_dir /*>;
my $count=@files;
Print "$count \n";

Thank you a lot for your inputs.
Ciao...
 
That counts everything, files and folders. If thats what you wanted thats what you should have said.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top