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.
Ops forgot to include the script.
#!/usr/bin/perl -w
use strict;
my $dir = "/interfaces/tdbank/test/";
opendir DIR, $dir;
You must always check opendir's return value.
my @files = readdir DIR;
Here is your problem, readdir returns only the filenames not the entire path. i.e. You must prepend $dir to these returned filenames or chdir to $dir to be able to access these files. One other option is to use the glob operator, this will give you the full pathnames.
my @files = <$dir/*>;
my %hash = ('HPAY' => 'file1' ,
'HRET' => 'file2');
for (my $i=0; $i<scalar(@files); $i++) {
You are used to C style coding, you can just write the loop construct as for my $file (@files) { #$file contains the filename
open FILE,"$files[$i]" || die "Unable to open file $files[$i] because
$!\n"; my $header = <FILE>;
print "header -->" , $header, "\n";
When you read in a line from a file, the newline comes along with it (actually $/ comes along with it, this variable is set to newline by default. perldoc perlvar). You must chomp of $/ (newline) before using it in this case as your hash keys don't have newlines in them.
chomp ($header);
close FILE;
Maybe you should add a check here to see if the header is actually a valid key in the hash
if (exists ($hash{$header})) {
#Perform your mv operation if this condition is true
my $command = "mv $files[$i] $hash{$header}";
print "command -->", $command, "\n";
system $command;
Make sure to check $? for the exit status of $command. perldoc -f system
}
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]