(I applied my own code style and removed unnecessary braces, using $_ instead of explicit loop variables may have worked and may be considered more 'perlish', but I don't want to think about which of my loop stomped on which others' $_ so I'm using explicit variables. Handles for files and dirs have been replaced with a simple $fh/$dh. If these aren't sufficient then that's a good indication one has to many of them open at the same time. It also caught a bug where i was referring to $directory instead of $path.)
#! /usr/bin/perl use strict; use warnings; # $ERRNO instead of $! amongst other stuff use English qw( -no_match_vars ); my $path = "./IN/"; my $string = 'ABC'; my @filesWithString; opendir my $dh, $path or die "Can not open $path: $ERRNO"; FILE: while (my $file = readdir $dh) { # ignore 'hidden' files and ., .. if ($file =~ m/^\./) { next FILE; } # opendir and this should be replace with File::Find # makes relocationg the perl script to a different directory # much easier my $filename = $path . $file; # always check return value for open, close open my $fh, '<', $filename or die "Can not open '$filename': $ERRNO"; # keep track of current line number for nice message my $linecount = 1; LINE: while (my $line = <$fh>) { # encase $string in \Q \E in case it contains regexp metachara +cters # (remove this if you actually want to use metachars) if ($line =~ m/\Q$string\E/) { print "Found string '$string' in file $filename (line $lin +ecount)\n"; push @filesWithString, $filename; # abort all further parsing - we already found the string last LINE; } $linecount++; } close $fh or die "Can not close filehandle for '$filename': $ERRNO"; } closedir $dh or die "Can not close $path: $ERRNO"; for my $file (@filesWithString) { my $newName = $file . '.ABC.txt'; print "Renaming $file to $newName\n"; # uncomment this after debugging / verification #rename $file, $newName; }

In reply to Re^2: Rename the text file WRT String by Monk::Thomas
in thread Rename the text file WRT String by reddevil420

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.