> Hi all
>
> I've this Perl script to read all files in a directory and
> if it finds a file with a certain then rename the file to
> header, but is doesn't search the right directory and doesn't
> even look for the HPAY header.
>
What format are these "headers" in in the file?
Assuming the files are relatively small text files (and to simplify the example)
I'd do:
[* untested by the way *]
use warnings;
use strict;
use File::Slurp;
for(read_dir($dir)) {
my $filegust = read_file($dir$_);
my ($hpay) = $fileguts =~ m/HPAY=(\w+)/;
if($hpay) {
write_file("$dir$hpay", $fileguts) or die $!;
unlink("$dir$_") or die $!;
}
}
- This will overwrite files already called $dir$hpay which may or may not be what you
want.
- There's probably a better way to move or rename the file but hey it works.
- no system() command to make it platform independent, easier to undertsand, etc etc
is good
HTH
Dmuey
> Ops forgot to include the script.
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my $dir = "/interfaces/tdbank/test/";
>
> opendir DIR, $dir;
>
> my @files = readdir DIR;
>
> my %hash = ('HPAY' => 'file1' ,
>
> 'HRET' => 'file2');
>
>
>
> for (my $i=0; $i<scalar(@files); $i++) {
>
> open FILE,"$files[$i]" || die "Unable to open file
> $files[$i] because $!\n";
> my $header = <FILE>;
>
> print "header -->" , $header, "\n";
>
> close FILE;
>
> my $command = "mv $files[$i] $hash{$header}";
>
> print "command -->", $command, "\n";
>
> system $command;
>
> }
>
>
> And I get the following Errors:
>
>
>
>
> Use of uninitialized value in concatenation (.) or string at
> ./process_f iles.sh line 14.
>
> command -->mv ..
>
> Usage: mv [-i | -f] [--] src target
>
> or: mv [-i | -f] [--] src1 ... srcN directory
>
> readline() on closed filehandle FILE at ./process_files.sh
> line 11.
> Use of uninitialized value in print at ./process_files.sh
> line 12.
> header -->
>
> Use of uninitialized value in hash element at
> ./process_files.sh line 14 .
>
> Use of uninitialized value in concatenation (.) or string at
> ./process_files.sh line 14.
>
> command -->mv file1
>
> Usage: mv [-i | -f] [--] src target
>
> or: mv [-i | -f] [--] src1 ... srcN directory
>
> readline() on closed filehandle FILE at ./process_files.sh
> line 11.
> Use of uninitialized value in print at ./process_files.sh
> line 12.
> header -->
>
> Use of uninitialized value in hash element at
> ./process_files.sh line 14 .
>
> Use of uninitialized value in concatenation (.) or string at
> ./process_files.sh line 14.
>
> command -->mv file2
>
> Usage: mv [-i | -f] [--] src target
>
> or: mv [-i | -f] [--] src1 ... srcN directory
>
> etrprodftp /usr/local/sbin
>
>
>
> Any help would be great..
>
>
> James Parsons.
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]