Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Permission Denied error when moving files - Perl

Status
Not open for further replies.

kcstulce

Technical User
Aug 2, 2004
8
US
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
 
Have you tried manually moving the files from the command line first? See if you have problems moving the files outside of your perl script.
 
Brian,

Thanks for your reply. I just moved the file through the command prompt: "move mcp_1678576.pdf .\1\mcp_1678576.pdf" and the file moved fine. I checked the permissions on the root folder and it's subfolder and I have permission for full control. Perl runs through my local login, correct?

Thanks again.

--Ken
 
Here's an updated version of the script, as I changed a couple of lines that I didn't particularly like:

###################
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)
{
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;
my $newfile;
unless (-d '$newpgcount')
{
mkdir($newpgcount);
$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
{
$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";
}
}
###################

Thanks,

--Ken
 
The directory level permissions may be alright, perhaps it is at the file level where your permissions issue is?

What is the exact error message you are getting?
 
ProbablyDown,

Here's the exact message from the command prompt:

############
D:\Scripts>perl Copy_pdf_pages.pl
File mcp_1678576.pdf has 3 pages
mcp_1678576.pdf:3
move( mcp_1678576.pdf, D:\Scripts\1\mcp_1678576.pdf ) failed: Permission denied at Copy_pdf_pages.pl line 61.
############

I check the permissions of the files and the folder and the permissions for my login and Administrators is both Full Control.

Thanks,

--Ken
 
Hmm... I would try to put the explicit path for the source file... another thing I might try is a copy instead of a move.. just to see if I get the same errors...
 
ProbablyDown,

Copy works great, but move/delete give the permission errors. If I could copy/delete that would work just as good as move, but no luck.

I'll try the explicit path and let you know of the results.

Thanks for your suggestions.

--Ken
 
I ended up discovering that Windows was holding the file open and wouldn't let me move the file. I had to perform the reading in a separate sub than the moving. I have it working now. Here's the code:

################
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;
my $PDFPages;
my $file;

foreach $pdfFile(@pdfname)
{
#print "$cwd\\$pdfFile\n";
do_the_dirty_job_on("$cwd\\$pdfFile");
pdfmove();
}

sub do_the_dirty_job_on
{
$file = shift;
#print "File:$file\n";
my $PDFfile = PDF->new($file);
if ($PDFfile->IsaPDF)
{
#print "File $file has ",$PDFfile->Pages," page",$PDFfile->Pages > 1 ? "s" :"","\n";
#print $file,":",$PDFfile->Pages,"\n";

$PDFPages = $PDFfile->Pages;
}
else
{
print "File $file isn't a PDF file\n";
}

}

sub pdfmove
{
my $newpgcount = $PDFPages -2;
my $newfile;
my @nameonly = split(/\\/,$file);
#print "NameOnly\: @nameonly\n";
my $filenameonly = $nameonly[2];
unless (-d $newpgcount)
{
mkdir("$newpgcount") or die "mkdir failed: $!";
$newfile = "$newpgcount/$filenameonly";
move($filenameonly,$newfile) or die "move($filenameonly, $newfile) failed: $!";
#File::Copy::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
{
$newfile = "$newpgcount/$filenameonly";
move($filenameonly,$newfile) or die "move($filenameonly, $newfile) failed: $!";
#File::Copy::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: $!";
}
}
############

BIG Thanks to everyone for their help!!

--Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top