I'm trying to use Perl for text processing for Latex. One of the big problems I have is spaces in file names in both in the current and sub directories for PDF files. I'm trying to use Perl to detect the PDF file in all the subdirectories and rename the file removing the spaces and putting in a dash instead.
I've got most of the program, I just cannot get the files
to copy. Any help would be appreciated.
The code I have is as follows:
----------------------------------------------
#!/usr/bin/perl -w
use warnings;
use strict;
use File::Find;
use File::Copy;
my @old;
my @new;
my %test;
find(\&find_pdfs, ".");
sub find_pdfs
{
my $file = $_;
chomp($file);
if ($file =~ /pdf$/) {
push @old,"$File::Find::dir/$_\n" if -f ;
}
}
@new = @old;
foreach my $i (@new) {
$i =~ s/ /\-/g;
}
@test{@old}=@new;
for (keys %test){
print "$_ and $test{$_}\n";
copy("$_","$test{$_}") or die "File cannot be copied.\n";
}
--------------------------------------
For example, in the current directory I have a file "J9161 plc.pdf". I want to change this to "J9161-plc.pdf".
Currently I can get the file name "./J9161 plc.pdf" and the replacement file name "./J9161-plc.pdf" but I get the following error when I use the copy command:
Unsuccessful stat on filename containing newline at c:/Perl/lib/File/Copy.pm line 112.
Any thoughts?
I've got most of the program, I just cannot get the files
to copy. Any help would be appreciated.
The code I have is as follows:
----------------------------------------------
#!/usr/bin/perl -w
use warnings;
use strict;
use File::Find;
use File::Copy;
my @old;
my @new;
my %test;
find(\&find_pdfs, ".");
sub find_pdfs
{
my $file = $_;
chomp($file);
if ($file =~ /pdf$/) {
push @old,"$File::Find::dir/$_\n" if -f ;
}
}
@new = @old;
foreach my $i (@new) {
$i =~ s/ /\-/g;
}
@test{@old}=@new;
for (keys %test){
print "$_ and $test{$_}\n";
copy("$_","$test{$_}") or die "File cannot be copied.\n";
}
--------------------------------------
For example, in the current directory I have a file "J9161 plc.pdf". I want to change this to "J9161-plc.pdf".
Currently I can get the file name "./J9161 plc.pdf" and the replacement file name "./J9161-plc.pdf" but I get the following error when I use the copy command:
Unsuccessful stat on filename containing newline at c:/Perl/lib/File/Copy.pm line 112.
Any thoughts?