in reply to Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.

Apart from what GrandFather said, I think you also want to use "chdir", like this:
my $directory = "/home/e/Doc/AutoMation"; chdir $directory; opendir( DIR, "." ); for my $file ( readdir DIR ) { next unless -f $file; open( my $in_fh, '<', $file ); … }
Either that, or else you'll need to include the directory name with the file name when you try to do anything with the file:
opendir( DIR, $directory ); for my $file ( readdir DIR ) { next unless -f "$directory/$file"; open( my $in_fh, '<', "$directory/$file" ); … }
  • Comment on Re: Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.
by Laurent_R (Canon) on Dec 23, 2014 at 13:04 UTC
    Or use the glob operator, rather than opendir and readir, which will remove the . and .. directories from the list and preserve the relative path of the entries. But one still has to check if the entries are really files with the -f operator.
Re^2: Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.
by hemantjsr (Initiate) on Dec 24, 2014 at 12:45 UTC

    Please find below Working Code. It's giving error message "Use of uninitialized value $lines1 in substitution (s///) a"

    apart from that it's giving a result like in a file01.txt have now file01.txt, in file02.txt have file02.txt only and so on. Old values are deleted and only filename are comeing file.

    where as i needed output like

    file01.txt have 11 10,9:10/4947000219 :20140924105028 24

    file02.txt have 11 10,9:10/4947000220 :20140924105228 24

    file03.txt have 11 10,9:10/4947000221 :20140924105428 24

    file04.txt have 11 10,9:10/4947000222 :20140924105628 24

    #!/usr/bin/perl use strict; use 5.10.0; # For autodie and regex \K use autodie; use File::Find; use Time::Piece; use Time::Seconds qw/ ONE_MINUTE /; use constant DATE_FORMAT => '%Y%m%d%H%M%S'; my $n; my $directory="/home/e/Doc/AutoMation"; chdir $directory; opendir(DIR, ".") or die "couldn't open $directory: $!\n"; foreach my $file (readdir DIR) { next unless -f $file; open my $in_fh, '<',$file; my @lines = $file; close $in_fh; ++$n; $lines[0] =~ s~/4947000219/\K(4947000210+)~$1+$n~e; $lines[1] =~ s{:20140924105028\K(20140924105028+)}{ my $tp = Time::Piece->strptime($1, DATE_FORMAT); ($tp + ONE_MINUTE * 2 * $n)->strftime(DATE_FORMAT); }e; open my $out_fh, '>', $file; print $out_fh @lines; close $out_fh; } closedir DIR;
      What did you expect to happen as a result of these lines?
      open my $in_fh, '<',$file; my @lines = $file; close $in_fh;
      If you expected that the contents of the data file would be read into the @lines array, the second line should be:
      my @lines = <$in_fh>;
      The way you posted it, you're just assigning the file name (value of $file) to be the first element of @lines, and nothing else is placed into the array. That's why you got "use of uninitialized value" when you tried to do something with the second element of the array.