Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Permission Denied error when moving files - Perl

Reply
Thread Tools

Permission Denied error when moving files - Perl

 
 
Ken
Guest
Posts: n/a
 
      11-10-2006
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)
{
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";
}
}

###########################

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

 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      11-10-2006
Ken wrote:
>[...] When trying to move/copy
> & delete I get a Permission Denied error from the script.
>
> Here's my script for you to review:


Since PDF isn't your issue, narrow down the problem by
removing all the extra code, which will simplify the
issue for you and us.

> my $newfile;
> unless (-d '$newpgcount')
> {
> mkdir($newpgcount);
> $newfile = "/$newpgcount/$file";


That initial '/' is probably the problem.

> move($file,$newfile) or die "move failed: $!";


To see the issue, change that to a more helpful error message:

move( $file, $newfile )
or die "move( $file, $newfile ) failed: $!";
 
Reply With Quote
 
 
 
 
Ken
Guest
Posts: n/a
 
      11-10-2006
J. Gleixner,

Thanks for your reply.

Per your advise, below is 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.
##########

Line 61 contains: move($file,$newfile) or die "move( $file, $newfile )
failed: $!";.
I changed line 60 to this: $newfile = "$newpgcount/$file";.

I have tried moving the file in a command prompt and that worked fine,
so I'm baffled why Perl is not able to move the file.

Any other suggestions?

Thanks again for your help.

--Ken

On Nov 10, 11:21 am, "J. Gleixner" <(E-Mail Removed)>
wrote:
Since PDF isn't your issue, narrow down the problem by
removing all the extra code, which will simplify the
issue for you and us.

> my $newfile;
> unless (-d '$newpgcount')
> {
> mkdir($newpgcount);
> $newfile = "/$newpgcount/$file";


That initial '/' is probably the problem.

> move($file,$newfile) or die "move failed: $!";


To see the issue, change that to a more helpful error message:

move( $file, $newfile )
or die "move( $file, $newfile ) failed: $!";

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-10-2006
Ken <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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");


There's no need to quote $cwd. See perldoc -q quoting.

Incidentally, you don't need the working directory explicitly, '.'
will do nicely.

> 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)


It's unnecessary to check for the existence of files. The loop below
will simply not run if there aren't any.

> {
> foreach $pdfFile(@pdfname)
> {
> do_the_dirty_job_on($pdfFile);

^^^^^^^^
This is probably your error. $pdfFile holds only the file name relative
to the directory $cwd. So to access the file, you'd need

do_the_dirty_job_on("$cwd/$pdfFile");

[rest snipped]

Anno
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      11-10-2006
Ken wrote:
> J. Gleixner,
>
> Thanks for your reply.


Sure. Thanks in advance for not top-posting again.

>
> Per your advise, below is 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.
> ##########
>
> Line 61 contains: move($file,$newfile) or die "move( $file, $newfile )
> failed: $!";.
> I changed line 60 to this: $newfile = "$newpgcount/$file";.
>
> I have tried moving the file in a command prompt and that worked fine,
> so I'm baffled why Perl is not able to move the file.


I took at look at your script again..

> unless (-d '$newpgcount')


Change that to:

unless( -d $newpgcount )

Also, if

$newpgcount=1;
$newfile = "$newpgcount/$file"

Then I'd think

die "move( $file, $newfile ) failed: $!";

Would print:

move( mcp_1678576.pdf, 1/mcp_1678576.pdf ) failed: Permission denied

but.. if the above change to your unless fails, then try something
simple, as I suggested before.

e.g. ensure the directory '1' exists and you have permissions to create
files there.

use File::Copy;
my $file = 'mcp_1678576.pdf';
my $newfile = "1/$file";
move( $file, $newfile )
or die "move( $file, $newfile ) failed: $!";
 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      11-10-2006
Anno,

Thanks for your reply.

In using your suggestions, the last item
"do_the_dirty_job_on("$cwd/$pdfFile");", ended up producing the file
error message from the CP:

############
D:\Scripts>perl Copy_pdf_pages.pl
File D:\Scripts\mcp_1678576.pdf has 3 pages
D:\Scripts\mcp_1678576.pdf:3
move(mcp_1678576.pdf, D:\Scripts\1\D:\Scripts\mcp_1678576.pdf) failed:
No such f
ile or directory at Copy_pdf_pages.pl line 75.

D:\Scripts>
############

Notice the destination move path:
"D:\Scripts\1\D:\Scripts\mcp_1678576.pdf".

In keeping with your suggestions, I began thinking that maybe Perl
needed the absolute path to pull in the pdf file for the subroutine,
but when moving, I only needed the file name, so I added another couple
of lines right before the point when I was going to move the file.
Here are the lines:

############
foreach $pdfFile(@pdfname)
{
#print "$cwd\\$pdfFile\n";
do_the_dirty_job_on("$cwd\\$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;
my @nameonly = split(/\\/,$file); ######### splits by \\
my $filenameonly = $nameonly[2]; ######### Grab only the
filename
unless (-d $newpgcount)
{
mkdir($newpgcount);
$newfile = "$cwd\\$newpgcount\\$filenameonly";
move($filenameonly,$newfile) or die "move($filenameonly, $newfile)
failed: $!";
}
else
{
$newfile = "$cwd\\$newpgcount\\$filenameonly";
move($filenameonly,$newfile) or die "move($filenameonly, $newfile)
failed: $!";
}
}
}
############

This line of thinking didn't work. This time I got my same original
error message.

Thanks again.

--Ken

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      11-10-2006


On Nov 10, 6:22 pm, (E-Mail Removed)-berlin.de wrote:
> Ken <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > do_the_dirty_job_on($pdfFile);

> ^^^^^^^^
> This is probably your error. $pdfFile holds only the file name relative
> to the directory $cwd. So to access the file, you'd need
>
> do_the_dirty_job_on("$cwd/$pdfFile");


But, Anno, the directory $cwd is the CWD.

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      11-10-2006
On Nov 10, 6:31 pm, "J. Gleixner" <(E-Mail Removed)>
wrote:
> Ken wrote:
>
> > unless (-d '$newpgcount')

>
> Change that to:
>
> unless( -d $newpgcount )


Better still get rid of it altogether since you are not bothering to
check if the mkdir() succeded anyhow just let it fail if the directory
already exists.

 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      11-10-2006
J. Gleixner,

Thanks again for your reply.

In trying your last suggestions, with a simple move script, this worked
perfectly. Now with using those same fundamentals, I need to figure
out where the problem is starting in my current script.

The one thing that is consistantly working, is the creation or the
checking on the "1" directory. The change to the Unless, didn't seem
to make a different, but I did leave off the single quotes.

This is just so bizarre. It's probably something so simple that's
being missed!

--Ken

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-10-2006
Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>
> On Nov 10, 6:22 pm, (E-Mail Removed)-berlin.de wrote:
> > Ken <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > > do_the_dirty_job_on($pdfFile);

> > ^^^^^^^^
> > This is probably your error. $pdfFile holds only the file name relative
> > to the directory $cwd. So to access the file, you'd need
> >
> > do_the_dirty_job_on("$cwd/$pdfFile");

>
> But, Anno, the directory $cwd is the CWD.


Oh, right. The only case where the file name doesn't need the directory
prefix.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
Permission denied at C:/Perl/site/lib/XML/Handler/YAWriter.pm line 43. Piet L. Perl Misc 1 02-24-2005 12:49 PM
Permission denied with PERL/APACHE Nicolas D Perl Misc 7 05-10-2004 11:46 PM
Using parent.blank permission denied error when running files locally Alexander Magidow Javascript 0 04-04-2004 08:42 PM
bash: /root/remstats.pl: /usr/bin/perl: bad interpreter: Permission denied Goblin Perl 1 08-14-2003 11:11 AM



Advertisments