Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating through Directories 1

Status
Not open for further replies.

ksbrace

Programmer
May 13, 2000
501
US
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!



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

 
Have a look at File::Find on search.cpan.org

HTH
--Paul
 
Paul,
I was looking at that module because it looked like the one I needed, but how do I specify that I only want .htm and .html files. Thanks.

 
raklet (MIS) Mar 4, 2004

Code:
use File::Find;

find (\&wanted, "root folder");

sub wanted {
   /\.htm$/ or return;  # use any regex here to match types of files
   -f and push @array, $File::Find::name;  #push only file names into an array
}
From raklet
 
I'd go one better and have a look at File::Find::Rule.
Code:
my $START = '/test/metalink/techb4/metalink/';
my $wanted = qr/.html?$/i;
my @wanted = File::Find::Rule->file()->name( $wanted )->in( $START );
Makes life alot easier than just File::Find IMHO :)

But then, there's always more than one way to do it ;-)

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top