Hello,
I'm trying to iterate through all files and subdirectories for .htm or .html. I have the changeTar method working properly, but the searchDirectory isn't working. I found a script to iterate through folders and tweaked it a bit, but it seems to be going through all files not just the "html or htm files. Any help would be greatly appreciated. Thanks in advance!
I'm trying to iterate through all files and subdirectories for .htm or .html. I have the changeTar method working properly, but the searchDirectory isn't working. I found a script to iterate through folders and tweaked it a bit, but it seems to be going through all files not just the "html or htm files. Any help would be greatly appreciated. Thanks in advance!
Code:
#!usr/local/bin/perl
$filename = "htm*";
# look in current directory
$v = `cd /test/metalink/techb4/metalink/`;
$dir = `pwd`;
chop($dir);
&searchDirectory($dir, 0);
sub searchDirectory {
local($dir);
local(@lines);
local($line);
local($file);
local($subdir);
$dir = $_[0];
# check for permission
if(-x $dir) {
# search this directory
@lines = `cd $dir; ls -l | grep $filename`;
foreach $line (@lines) {
$line =~ /\s+(\S+)$/;
$file = $1;
&changeTars($file);
# search any sub directories
@lines = `cd $dir; ls -l`;
foreach $line (@lines) {
if($line =~ /^d/) {
$line =~ /\s+(\S+)$/;
$subdir = $dir."/".$1;
&searchDirectory($subdir);
}
}
}
}
}#searchDirectory
#
sub changeTars( $tFile ) {
my ($input) = @_;
my $id = '3188923';
open(INPUT, "<$input"); # open the file to read it
@data = <INPUT>; # read the whole file into an array
close(INPUT); # close the file
open (OUTPUT, ">$input"); # now open it to write to
flock(OUTPUT, 2); # and lock it
for ($i=0;$i<$#data+1;$i++) { # for each line in the array
chomp($data[$i]); # get rid of any trailing new-line character
# if it matches the data I'm looking for
if( $id eq $data[$i] ) {
print "Found $id in $file inside $dir\n";
#$counter++;
#$data[$i] = 'NEW DATA ';
#print "data changed in $input\n$data[$i]\n";
print OUTPUT "$data[$i]\n"; # write the changed line out to the file
}#if
else { # otherwise
print OUTPUT "$data[$i]\n"; # write the unchanged line out to the file
}#else
}#for
print "Found $id in: $input";
flock(OUTPUT, 8); # unlock the file
close(OUTPUT); # and close it
}#changeTars