Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > traverse a directory and subdirectories to find files ' properties

Reply
Thread Tools

traverse a directory and subdirectories to find files ' properties

 
 
Thieum22
Guest
Posts: n/a
 
      08-06-2004
Hi,

I try to go through a directory and it's subdirectories to reah the
properties of each files. But I have a problem to set active the
directory where the files are, in order to display their properties.

If anyone know how to improve my method, or know a better one, he's
welcome

Thanks,

Matthieu

here is the code :

#! /usr/bin/perl

use File::Find;

$dir_tree = '/tmp'; #test directory
# A good way to go through the subdirectories
finddepth(\&ren_dir, $dir_tree);

sub ren_dir {
next unless (-d);
print "where are we : ";
print($_ );
print "\n";

# We set the homedir
opendir(HOMEDIR, $_) || die("unable to open directory");
# chdir ($_); # make the program active only in the first
directory parsed -> why not the other ones ?
print "* we are in a directory";
print "\n";
while($filename = readdir(HOMEDIR))
{
if ($filename ne "." && $filename ne "..")
{
#we are testingif it is a file
if (-f $filename) {
print "* we've find a file";
print "\n";

print($filename);
print "\n";
# file properties
$mtime = 0;
$size = 0;
($dev, $ino, $mode, $nlink, $uid,
$gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat
$filename;
print " for example : last time
modified : ";
print($mtime);
print "\n";

}

}
}
closedir(HOMEDIR);
}
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      08-06-2004
Thieum22 wrote:

> Hi,
>
> I try to go through a directory and it's subdirectories to reah the
> properties of each files. But I have a problem to set active the
> directory where the files are, in order to display their properties.
>
> If anyone know how to improve my method, or know a better one, he's
> welcome


Don't use opendir() and readdir(); let find() do that for you.

linux% find2perl /tmp -ls >find_example.pl
linux% perl find_example.pl
linux% cat find_example.pl

Note where it is using $File::Find::name, which is the combination
of $File::Find::dir and $_.
-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
noob question:traverse directory and populate mysql db jay Ruby 0 02-01-2007 09:52 AM
how to use Perl to rename the filenames and directory names under current and subdirectories recursively? lucy Perl Misc 6 09-03-2004 06:27 AM
subdirectories and asp.net, and any configuration to reference a parent directory's bin dir. Steve Haun ASP .Net 0 11-16-2003 03:25 AM



Advertisments