windows - Perl script to download a file and check filetype -


what want simple: 1) download file hard-coded url (it stay same) 2) check if file pdf (i thought best check magic number %pdf) 3) if so, rename file , move folder xy||if not, rename accordingly.

note: required run on windows 7 system.

here code:

#!/usr/bin/perl use warnings; use strict; use lwp::simple; use posix qw( strftime ); use file::copy; use file::type;  $date = strftime("%m/%d/%y", localtime); $url  = "http://www.oracle.com/technetwork/database/options"           ."/advanced-analytics/r-enterprise/ore-reference-manual-1882822.pdf";   $dlfile = "test.pdf"; $resp = ''; $srcdir = "c:\\pdfscripthome"; $dest = "c:\\pdfdump"; $old = "$srcdir/$dlfile"; $resp = getstore( $url, $dlfile );  sub checkfiletype {     $chkfile="$srcdir//test.pdf";     $ft = file::type->new();     $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 try execute, nothing happens. strangely, when comment

use file::type; 

out, downloads file (of course check doesn't happen).

i assume there error somewhere in sub checkfiletype { } can't see it.

i executed perl program (without changes) on windows 7 strawberry perl v5.20.2.

i tempted suggest several changes, decided instead offer 1 simple change cause program complete successfully.

in call mime_type() on line 25, replace argument $chkfile $dlfile. cause mime_type() file stored during call getstore() on line 16, $dlfile passed argument specifying 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, perl program first stores downloaded pdf file relative user's current working directory filename .\test.pdf, later expects find pdf file using absolute path c:\pdfscripthome\test.pdf.

if user's current working directory c:\pdfscripthome program succeeds. in cases user's current working directory not c:\pdfscripthome, program fails because argument passed in call mime_type() contains absolute path c:\pdfscripthome\test.pdf.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -