in reply to Search and replace copyright to .java scripts

I guess the best way would be:
1. Read in the new copyright file.
2. List the directory where the .java files are.
2a. Open each one
2b. Write the contents to a new file until you find a chunk of text starting with /* and containing the 'Copyright (c) .. ' text
2c. Replace the old bits with the new bits from the copyright file read in earlier.
2d. Write the rest of the file. Close it.
3. Optionally, rename new file to old filename.

Thats Psuedocode for it. If you need help on specific bits, then please post what you have tried so we can see where it's going wrong.

C.

  • Comment on Re: Search and replace copyright to .java scripts

Replies are listed 'Best First'.
Re: Re: Search and replace copyright to .java scripts
by Anonymous Monk on Feb 03, 2003 at 17:16 UTC
    I use the following code to searches for the .java scripts and print the date line (1999 - 2002) to see how many scrpts out of date. What I would like is to adapt this to change the copyright.
    my $root = 'f:/'; my %hash = (); # # main # { my @files = getFileList(); # find files within given vobs my $n = @files ; print STDERR "Searching $n files\n" ; my $n = 0 ; foreach my $f (@files) # open found files { $n++ ; print STDERR "$n\r" ; unless (open( SRC, $f )) { print STDERR "Unable to open $f: $!\n" ; next ; } while (<SRC>) { last if (/^\s*package/); #next unless (/\$Workfile/m); # get filename next unless (/copyright.*application/i); # get copyright chomp ; # remove newline s/^\s*\*//; # remove comments s/\s+/ /; # multiple whitespace into single whitespac +e s/^\s+// ; # remove any leading whitespace s/\s+$// ; # remove any trailing whitespace $hash{ $_ }++; last ; } close (SRC); } foreach my $k (sort keys %hash) { my $v = $hash{ $k }; #print STDERR "$v: $k\n" ; print STDERR "$_\n" ; } } my @theFiles = (); sub getFileList() { my @files = (); foreach my $k ("dir1", "dir2") { @theFiles = (); find( \&wanted, "$view/$k/src" ); push @files, @theFiles ; } return @files ; } sub wanted { return unless (-f $_ ); return unless ($_ =~ /\.java$/); push @theFiles, $File::Find::name ; }
      # perl -pi -e 's/oldcopyright/newcopyright/ig' `find . -type f -name *.java -print`

      (Note the backticks.)