Perl unzip / dynamic naming and error handling -


i ran bit of snag long running (and working) script , have come conclusion there needs better approach problem.

once day file emailed generic account. postfix hands off script processing, looks file attachments , saves them off. 99.9% of time there's 1 attachment, zip file, , it's sent .doc extension through spam filter.

sometimes, however, file sent 'file.doc' other times 'file.doc' , still others 'file.doc.zip' or 'file.doc.zip' in each case there's 1 file attached, in order handle these scenarios script looks this:

if ( </tmp/*.doc> ){         unzip '</tmp/*.doc>' => '</media/win/#1.txt>'                 or die "unzip failed: $unziperror\n"; }  if ( </tmp/*.doc> ){         unzip '</tmp/*.doc>' => '</media/win/#1.txt>'             or die "unzip failed: $unziperror\n"; } 

is there better way handle these possible conditions filenaming can't control? .zip, .zip, .doc, .doc, or whatever else human decides type day despite clear instructions. (isn't case?" know how large file - unzip based on that, or find other non-filename way it?

you can grep files with .doc , .zip extensions ignoring case , need them:

opendir(dir, '/tmp/'); @files = grep {/\.(doc|zip)$/i} readdir(dir); closedir dir;  foreach $file (@files) {     #do need each file } 

Comments