Hello,
I have a script that is reading the contents of a directory, only for files with a *.PDF extension. Once the files names are read into a list, the pdf's are looked at to determine the number of pages in the file. Based on the number of pages, I'm wanting a new directory to be created or used, if it already exists, and move the pdf files with the matching number of pages into the directory. When trying to move/copy & delete I get a Permission Denied error from the script.
Here's my script for you to review:
###########################
use strict;
use warnings;
use File::chmod;
use Cwd;
use Carp;
use Getopt::Long;
use PDF;
use POSIX;
use File::Copy;
use File::Basename;
# find out current working directory
my $cwd = getcwd;
#print "$cwd\n";
opendir(DIR, "$cwd") || die ("Unable to open directory");
my @pdfname = grep { /\.pdf$/ } readdir(DIR);
closedir(DIR);
# Find all files with "*.pdf" extension
#my @pdfname = glob("*.pdf");
my $pdfcount = @pdfname;
my $pdfFile;
if($pdfcount != 0)
{
foreach $pdfFile(@pdfname)
{
do_the_dirty_job_on($pdfFile);
}
}
exit(1);
sub do_the_dirty_job_on
{
my $file = shift;
my $PDFfile = PDF->new($file);
if ($PDFfile->IsaPDF)
{
$verbose ? print "File $file has ",$PDFfile->Pages," page",$PDFfile->Pages > 1 ? "s" :"","\n"
: print $file,":",$PDFfile->Pages,"\n" ;
my $PDFPages = $PDFfile->Pages;
my $newpgcount = $PDFPages -2;
unless (-d '$newpgcount')
{
mkdir($newpgcount);
my $newfile = "/$newpgcount/$file";
move($file,$newfile) or die "move failed: $!";
#rename($file,$newfile) or die "move failed: $!";
#system("move","$file","/$newpgcount/$file") or die "move failed: $!";
#unlink($file) or die "delete failed: $!";
}
else
{
my $newfile = "/$newpgcount/$file";
move($file,$newfile) or die "move failed: $!";
#rename($file,$newfile) or die "move failed: $!";
#system("move","$file","/$newpgcount/$file") or die "move failed: $!";
#unlink($file) or die "delete failed: $!";
}
}
else
{
print "File $file isn't a PDF file\n";
}
}
###########################
I have left in the commented lines that I have tried for you to see the different steps that I have taken.
Thanks in advance for your help and advise.
--Ken
I have a script that is reading the contents of a directory, only for files with a *.PDF extension. Once the files names are read into a list, the pdf's are looked at to determine the number of pages in the file. Based on the number of pages, I'm wanting a new directory to be created or used, if it already exists, and move the pdf files with the matching number of pages into the directory. When trying to move/copy & delete I get a Permission Denied error from the script.
Here's my script for you to review:
###########################
use strict;
use warnings;
use File::chmod;
use Cwd;
use Carp;
use Getopt::Long;
use PDF;
use POSIX;
use File::Copy;
use File::Basename;
# find out current working directory
my $cwd = getcwd;
#print "$cwd\n";
opendir(DIR, "$cwd") || die ("Unable to open directory");
my @pdfname = grep { /\.pdf$/ } readdir(DIR);
closedir(DIR);
# Find all files with "*.pdf" extension
#my @pdfname = glob("*.pdf");
my $pdfcount = @pdfname;
my $pdfFile;
if($pdfcount != 0)
{
foreach $pdfFile(@pdfname)
{
do_the_dirty_job_on($pdfFile);
}
}
exit(1);
sub do_the_dirty_job_on
{
my $file = shift;
my $PDFfile = PDF->new($file);
if ($PDFfile->IsaPDF)
{
$verbose ? print "File $file has ",$PDFfile->Pages," page",$PDFfile->Pages > 1 ? "s" :"","\n"
: print $file,":",$PDFfile->Pages,"\n" ;
my $PDFPages = $PDFfile->Pages;
my $newpgcount = $PDFPages -2;
unless (-d '$newpgcount')
{
mkdir($newpgcount);
my $newfile = "/$newpgcount/$file";
move($file,$newfile) or die "move failed: $!";
#rename($file,$newfile) or die "move failed: $!";
#system("move","$file","/$newpgcount/$file") or die "move failed: $!";
#unlink($file) or die "delete failed: $!";
}
else
{
my $newfile = "/$newpgcount/$file";
move($file,$newfile) or die "move failed: $!";
#rename($file,$newfile) or die "move failed: $!";
#system("move","$file","/$newpgcount/$file") or die "move failed: $!";
#unlink($file) or die "delete failed: $!";
}
}
else
{
print "File $file isn't a PDF file\n";
}
}
###########################
I have left in the commented lines that I have tried for you to see the different steps that I have taken.
Thanks in advance for your help and advise.
--Ken