in reply to go through directories recursively

I'd like to point you to a posting I made earlier today for a similar problem: (see 326175)

This code keeps a array of directories and works through them sequentially. Finding a dir somewhere adds it to that array.
pelagic

Replies are listed 'Best First'.
Re: Re: go through directories recursively
by danield (Novice) on Feb 03, 2004 at 18:41 UTC
    I did check your suggestion, and the code now looks like this:
    #!/usr/bin/perl -w #!C:\Perl\bin\perl -w my $directory='/data/input/data.01.29.2004-15:34:19/content'; my $size=0; my $file; #opendir(DIRHANDLE, $directory) or die "couldn't open $directory : $!\ +n"; #my@files = grep(!/^\.\.?$/,readdir(DIRHANDLE)); push(@dirs,$directory); # add an element to an array foreach $dir (@dirs) # loop through the elements of an array { opendir(DIR,"$dir") || die "opendir $dir failed"; my@list = grep(!/^\.\.?$/,readdir(DIR)); closedir(DIR); foreach $file (@list) { $size=(-s "$dir/$file"); print "$directory,$file,$size"; print "\n"; } }; #closedir(DIRHANDLE);

    However it still does not do what I want. The script now gives a list of subdirectories of 'content' directory. Each subdirectory however contains files I am interested in. Example:

    /data/input/data.01.29.2004-15:34:19/content/1/file001.txt
    ..
    /data/input/data.01.29.2004-15:34:19/content/1/file100.txt
    /data/input/data.01.29.2004-15:34:19/content/2/file001.txt
    ..
    /data/input/data.01.29.2004-15:34:19/content/2/file100.txt
    ..

    What I get is:

    /data/input/data.01.29.2004-15:34:19/content,1,8192
    /data/input/data.01.29.2004-15:34:19/content,2,8192
      ok, you dropped an important part and you had a couple of typos also. Like that it works:
      #!/usr/bin/perl -w #!C:\Perl\bin\perl -w my $directory='C:\dev\perl'; my $size=0; my $file; #opendir(DIRHANDLE, $directory) or die "couldn't open $directory : $!\ +n"; #my@files = grep(!/^\.\.?$/,readdir(DIRHANDLE)); push(@dirs,$directory); # add INITIAL directory to an array foreach $dir (@dirs) # loop through the elements of an array { opendir(DIR,"$dir") || die "opendir $dir failed"; my@list = grep(!/^\.\.?$/,readdir(DIR)); closedir(DIR); foreach $file (@list) { if (-d "$dir/$file") { push(@dirs, "$directory/$file"); # if this is a dir ad +d it to an array } if (-f "$dir/$file") { # only if it a file do your +size stuff $size=(-s "$dir/$file"); print "$dir,$file,$size"; print "\n"; } } }; #closedir(DIRHANDLE);

      see comments in code ...
      pelagic