Reputation: 21
What I want to do is fairly simple: 1) download a file from hard-coded URL (it will always stay the same) 2) check if the file really is a PDF (I thought that it is best to check the magic number %PDF) 3) if so, rename file and move to folder xy||if not, rename accordingly.
Note: this will be required to run on a Windows 7 system.
Here is my code:
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use POSIX qw( strftime );
use File::Copy;
use File::Type;
my $date = strftime("%m/%d/%Y", localtime);
my $url = "http://www.oracle.com/technetwork/database/options"
."/advanced-analytics/r-enterprise/ore-reference-manual-1882822.pdf";
my $dlfile = "Test.pdf";
my $resp = '';
my $srcdir = "C:\\pdfscripthome";
my $dest = "C:\\pdfdump";
my $old = "$srcdir/$dlfile";
$resp = getstore( $url, $dlfile );
sub CheckFileType {
my $chkfile="$srcdir//Test.pdf";
my $ft = File::Type->new();
my $file_type = $ft->mime_type($chkfile);
if ( $file_type eq 'application/pdf' ) {
move( $old, $dest ) or die "Move $old -> $dest failed: $!";
}
else {
rename ("//$srcdir/Test.pdf", "//$srcdir/NotAValidPDFFile.pdf" )
|| die ("Error in renaming");
}
}
sub main{
&CheckFileType();
}
&main;
What happens when I try to execute, is that nothing happens. Strangely, when I comment
use File::Type;
out, it downloads the file (of course the check doesn't happen).
I assume there is an error somewhere in sub CheckFileType { }
but I can't see it.
Upvotes: 0
Views: 1053
Reputation: 21
I successfully executed your Perl program (without changes) on Windows 7 with Strawberry Perl v5.20.2.
I was tempted to suggest several changes, but I decided instead to offer one simple change which will cause your program to complete successfully.
In the call to mime_type()
on line 25, replace the argument $chkfile
with $dlfile
. This will cause mime_type()
to look for the file stored during the call to getstore()
on line 16, where $dlfile
is passed as an argument specifying the filename.
Line 25 (original):
my $file_type = $ft->mime_type($chkfile);
Line 25 (incorporating suggested change):
my $file_type = $ft->mime_type($dlfile);
Without changes, your Perl program first stores the downloaded PDF file relative to the user's current working directory as the filename .\Test.pdf
, then later expects to find the PDF file using the absolute path C:\pdfscripthome\Test.pdf
.
If the user's current working directory is C:\pdfscripthome
the program succeeds. In cases where the user's current working directory is not C:\pdfscripthome
, the program fails because the argument passed in the call to mime_type()
contains the absolute path C:\pdfscripthome\Test.pdf
.
Upvotes: 2