need some hints

David Jacoby jacoby at csociety.purdue.edu
Sun Sep 24 15:48:31 CDT 2000


On Sun, 24 Sep 2000, eka.kevin.k1 wrote:

> Hi all,
> 
> I am pretty new  to perl
> and I'd like to do the following:
> 1. I am in any current directory, let say 'cur'
>    cur has about 20 folders that contain .txt files and .doc files and
> etc
> 2. want to find all *.txt files under each folder/directory 

recursive_thing "cur" ;

sub recursive_thing {
  my $dir = shift ;
  opendir  DIR , "$dir" ;
  while ( defined ( $file = readdir ( DIR ) ) ) {
    push @dirs , $file if $file =~ /\.txt$/ ;
    recursive_thing $dir if -d $dir ;
# There may be the need for doing things like setting cwd or such to
# keep position.
    }
  closedir DIR ;
  }

> 3. for each folder, I want to concatenate all the .TXT  files 
>    to one file : let say file001.txt  until file020.txt 
>    that will be placed in the (original) current directory cur

open  FILE1 , ">>../$dirname" ;

for $file ( sort @files ) {
  open  FILE2 , "$file" ;
  while (<FILE2>) { print FILE1 $_ ; } 
  close FILE2 ;
  }

close FILE1 ;

> 4. delete all files in all folders under the current directory cur
>    ( just the files in the folders, not the folder)

Unlink $file ;

> Can anybody give me a hint on this?
> I am not sure to do this on UNIX or Windows; 
> if that matters, I can just copy the files to the purdue account 
> and run it from there.

The one thing that I can think of is; opendir tends to give an
alphabetically-sorted result in Windows but not in Unix, and I 
have not been able to get -d to work for what I've done in Windows, 
which has been under perlscript using ASP, so it might not be a
representative example.

> Please give me suggestions since I have never written in perl before.
> 
> Thanks!
> Kevin

-- 
Dave Jacoby    jacoby at csociety.purdue.edu




More information about the Purdue-pm mailing list