Hi,

I have a script that search my pc for folder or file that contain a certain substring and update them. The script also open files and search for the same substring and update them. The problem is my script take more than an hour to finish running and I suspect that it has something to do with the alogarithm.

Here is the script:

#!/usr/perl/bin #=============================================================# # #This script traverse directory updating files and directories names # #==============================================================# use strict; use File::Find; use Win32::AdminMisc; use Sys::Hostname; my $old_hostname = hostname(); #current hostname my ($new,$orig,$changed,$concat,$new_hostname, $result,$f,$file, $str, +$ln,$line, @files,$linr ); my @lines = " "; my @files; my @fixedDrives = Win32::AdminMisc::GetDrives(DRIVE_FIXED); ## Create new hostname based on physical address ### print "Current host name of machine is :$old_hostname \n"; $new_hostname = "csscs_ref"; print "The new host name is going to be: $new_hostname\n"; #--------------------------------------------------------------------- +----------------------------------------------------- print "Looking for files to rename \n"; finddepth (\&rename_file_dir , @fixedDrives ); finddepth (\&modified_file , @fixedDrives ); #--------------------------------------------------------------------- +------------------------------------------------------- sub rename_file_dir { if( /(.*)$old_hostname(.*)/i )# any files or directories { $orig = $File::Find::name ; print "rename_file_dir : $orig\n"; $concat = $File::Find::dir . "\/$1$new_hostname$2"; rename($orig, $concat) || print "error can't rename $orig to + $concat: $!" } + + } # end of sub update_file #--------------------------------------------------------------------- +---------------------------------------------------- sub modified_file { if (-f "$File::Find::name" and (! -B "$File::Find::name")){ print "modified_file sub : $File::Find::name\n"; open (IN,"$File::Find::name"); @lines = <IN>; close IN; @lines = map { s/\b$old_hostname\b/$new_hostname/sgi; $_ } @ +lines; open (OUT,">$File::Find::name") || "can't open $File::Find:: +name for writing :$! "; print OUT @lines; } }
If can someone point out my error or show me a better way I would be grateful.

--thanks


In reply to Search and update scripts by kirk123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.