Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Rename the text file WRT String

by Monk::Thomas (Friar)
on Jul 24, 2013 at 08:08 UTC ( #1046031=note: print w/replies, xml ) Need Help??

in reply to Re: Rename the text file WRT String
in thread Rename the text file WRT String

(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; }

Replies are listed 'Best First'.
Re^3: Rename the text file WRT String
by mtmcc (Hermit) on Jul 24, 2013 at 10:08 UTC
    Thank you for restyling my working code. I'm sure reddevil420 will find your styling helpful. By the way, as far as I'm aware, the behaviour of rename is system dependent, which is why I prefer not to use it.

      a) I failed to recognize it wasn't the OP who posted this code. Ahem. Oops.

      b) I'm aware that rename has portability issues, but in this case it's pretty safe to use it because the requirements specify the rename must happen to a file and the file name changes, but not the location. Since we closed the filehandle before, the 'open file' issue does not apply either. Other issues may apply, but this is the OPs responsibility to figure out since we do not know the environment and requirements.

      c) nitpick: Your manual rename didn't since there was no unlinking of the old file. I'm sorry to say that but it was just a very inefficient copy, which may even silently generate garbage output in case the disk space gets exhausted while copying. (The OP didn't give any clue how large the files are.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046031]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-07-23 17:46 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (347 votes). Check out past polls.