posted 10-23-2002 02:20 PM
You can use this Perl script if you want to count lines for all of your WinRunner scripts living in a project directory. It should find any file named "script" living within 2 levels deep of the path you specify.-keigo
#!perl -w
#
# Counts the number of lines specified in a directory of scripts
# (Does not count comments or blank lines)
# You can use this on a directory that is one level higher so that
# you can count the lines of an entire group of scripts.
#
# Created: 2002/10/23 by Keigo Goto
# Usage: perl WRLineCounter.pl <directory path>
#
#
use strict;
if (! $ARGV[0]) {
print("USAGE: perl WRLineCounter.pl <directory of scripts>");
exit(0);
}
my $counter = 0;
my $directory = $ARGV[0];
## add a slash if needed
my $lastChar = substr($directory, (length $directory) -1, 1);
if ($lastChar ne "/" | | $lastChar ne "\\") {
$directory = $directory."\\";
}
my @files;
### Gather the files we will parse and count lines.
opendir DIR, "$directory" or die "Error: Please specify a directory to check.";
while ($_ = readdir(DIR)) {
next if $_ eq "." or $_ eq "..";
if (-d $directory.$_) {
my $scriptFile = $directory.$_."\\script";
if (-f $scriptFile) {
push(@files, $scriptFile);
}
}
# also run for a single directory
elsif (-f $_ and $_=~/script/) {
push(@files, $_);
}
}
my $directory_str_length = length $directory;
### parse each file and increment counter for each good line.
foreach my $scriptFile (@files) {
open SCRIPT, "< $scriptFile" or die "Couldn't open the file $scriptFile";
## subCounter holds number of lines for each file.
my $subCounter = 0;
while (<SCRIPT> ) {
next if (($_ =~ /^[\s]*[#]/) or ($_ =~ /^\s*\n/));
$counter++;
$subCounter++;
}
my $displayScriptName = substr($scriptFile, $directory_str_length);
print $displayScriptName.": $subCounter lines\n";
}
### Print out the results
my $numOfFiles = scalar @files;
print "Number of files to parse: $numOfFiles\n";
print "Total Number of Lines of Code: $counter \n";