Hi,

Does anyone know how to browse through a folder to find all the files contains in it? I know how to use the opendir function but:

1. I'm having problem with looping in which i can use to browse through ALL the folders and ALL the files contained in it.

2. What should i do to print our the directory of the current file?

Thanks.

Anyway, here's my code so far:

#/usr/bin/perl
#browse.pl

use warnings;
use strict;

print "Files of the current directory:\n";
opendir DH, "C:\\Program Files\\Player\\" or die "Couldn't open the current directory: $!";
while ($_ = readdir (DH))
{
    print $_, " " x (30-length($_));
}
use strict;
use warnings;
use Cwd;

use DirHandle;
my $folder = 'C:\Perl5.8.8\site\lib\File';
my $dh = DirHandle->new($folder);
while(defined(my $val = $dh->read)){
	print "\nDirectory: ",$folder."\\".$val if (-d $folder."\\".$val);
	print "\nFile: ",$folder."\\".$val if (-f $folder."\\".$val);
}
$dh->close();

print "\n\nCurrent working dir: ", getcwd();

katharnakh.

I think he wants to use File::Find to browse all files in all sub folders.

basic script with no formatting of output:

use warnings;
use strict;
use File::Find;

use File::Find;
    find( {wanted=> \&wanted, no_chdir => 1}, 'C:/Program Files/Player' );
    sub wanted { print $_,"\n"; }

See File::Find (a core module) for details.

commented: good! +1

Thanks Kevin, that's is exactly what i'm looking for! I'm trying to understand the coding. But if you don't mind, can u briefly explain the usage of File :: Find. Thanks a lot! : ) And thanks katharnakh as well but the coding you gave is something like mine. Can't browse through all the files. Using the depth-first-search like find is better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.