Hi,
If I may give you a head up. There are several ways to achieving your aim and this is one of them.
- Using the module File::Find, you can transverse the whole of the directory ( or directories ), then
- get each of the file, one after another, read through, using a while loop, within an open function, then do whatever you want!
Please see an example below.
use warnings;
use strict;
use File::Find qw(find);
use File::Basename qw(basename);
use Carp qw(croak);
croak "Usage: perlscript.pl <drectory_name>" unless defined $ARGV[0];
my $directory_name = $ARGV[0];
find( \&work_n_save, $directory_name ); # transverse the directory for
+ each file
sub work_n_save {
return if $_ eq '.' or $_ eq '..';
my $filename = $File::Find::name if !-d; ## get file name if not
+ directory
$filename = basename($filename); ## get the file base na
+me
$filename =~ s/\..+$//; ## remove the file exte
+ntion
open my $fh_new, '>', $filename . "_new.txt" or croak "can't open
+file: $!";
open my $fh, '<', $_ or croak "can't open file: $!";
while ( defined( my $line = <$fh> ) ) {
chomp $line;
## do whatever you want
print {$fh_new} $line, $/;
}
}
NOTE: That the script above will save the new file generated
within the same directory, where the original files are.
If you don't want that you might have to create another dircetory and
move your new file there!( easy too! )
Try/Test this on any of directory you have and see what you get, then re-write to what you want!
Check
perldoc File::Find for detail info.
Hope it helps.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.